psychopy.org | Reference | Downloads | Github

Unable browser-specific keyboard shortcuts

In my experiments, subjects are asked to answer a question by using a text input field coded as shown here.

I noticed that Firefox ( and I don’t know if Chrome does that too) has a keyboard shortcut enabled, that uses the backspace to go back to the previous page. So, it may happen that the while typing their answer, subjects may need to use the backspace to edit it. I would like to avoid the possibility the using the back space may make them go back to the previous webpage instead of deleting the text they just typed.

Is there a way I could disable browser-specific keyboard shortcuts in the script of my experiment?

@rob-linguistics13

Hi Rob,

I also run linguistics experiments using typed input. I can’t seem to see the text input field code. Could you try posting it again? I would also like to safeguard against this issue.

Jordan

1 Like

Thanks for spotting the missed link, @unagi_pie! Now it should work.

Interesting idea. I’m sure there must be JS code to disable the back shortcuts. There are a couple of suggestions below, for example. We haven’t implemented a method to do this within PsychoPy/PsychoJS as yet

1 Like

Thanks for the link. I have a very limited JS knowledge, so I am not going to test any of the solutions suggested in it. But it would be a useful option to have. I just wonder how hard it would be to implement it in PsychoJS, so that it works cross-platform…

@rob-linguistics13

I wonder if this will help.

When I was developing my typing routines, I noticed that I was accidentally disabling the ESC from experiment key. So, I had to add a line that would allow participants to withdraw from the experiment (exit) even if they were in the middle of typing. So, I manually added the line telling the program what to do when the ESC button gets pressed. I wonder if the same would be true of BACKSPACE. If it’s nested inside a while loop, it might prevent the browser from executing other functions of the backspace key.

typedText.setText(('>>>' + inputText));

theseKeys = psychoJS.eventManager.getKeys()

i = 0
n = theseKeys.length

while (i < n) {
    if (theseKeys[0] == "space") {
        inputText += " "
        i += 1
    } else if (theseKeys[0] == "backspace") {
        inputText = inputText.slice(0,-1)
        i += 1
    } else if (theseKeys[0] == "return") {
        continueRoutine = false 
        break
    } else if (theseKeys[0] == "escape") {
        psychoJS.quit()
        break
    } else if (theseKeys[0].length > 1) {
        i += 1
    } else {
        inputText += theseKeys[0]
        i += 1
    }
}

if (i == 1) {
    psychoJS.experiment.addData('letterTyped', theseKeys[0])
    psychoJS.experiment.addData('letterTypingRT', clock.getTime())
    psychoJS.experiment.nextEntry()
}   

Jordan

Interesting. I’ll give it a try, thanks!

The following code worked for me (pasted in the Each Frame tab of my code component, which may or may not be the best placement):

document.onkeydown = function (event) {
	
	if (!event) { /* This will happen in IE */
		event = window.event;
	}
		
	var keyCode = event.keyCode;
	
	if (keyCode == 8 &&
		((event.target || event.srcElement).tagName != "TEXTAREA") && 
		((event.target || event.srcElement).tagName != "INPUT")) { 
		
		if (navigator.userAgent.toLowerCase().indexOf("msie") == -1) {
			event.stopPropagation();
		} else {
			alert("prevented");
			event.returnValue = false;
		}
		
		return false;
	}
};	

I found this online, and I didn’t have to change any of my original coding to get it to work - i.e., my JS is set up to delete the last character when backspace is pressed, and this still works fine whilst avoiding the Firefox shortcut.

Jamie