问题
I have a Sudoku puzzle and I want to make it so the user can press the shift key and hold it down while they use the arrow keys to move up and down between cells. The cells that are already set are <input>
tags like the rest, but they have the readonly
attribute. Each cell has a class of 'puzzle_cells' and an id starting with a 'c' followed directly by the row number they are in, a '-', and then the cell number.
So, the third cell down, fifth from the left would have an id of 'c3-5'. Every cell has an id, so I'm thinking the inputs that are editable would have an attribute something like this onkeyup="shiftArrows(this)"
. My question is what do I put in shiftArrows()
?
It probably would start with something like:
var input_id = $(this).attr('id');
but then I have to test if an arrow key is being pressed while the shift key is down. If shift+down
is pressed or shift+up
and the cell directly above or below it is 'readonly'
, then it would go to the right one and check it again. If that is 'readonly'
, it would go to the left two, and would keep checking like that. if shift+right
is pressed it would keep going to the right until if finds an empty one, or jump to the next line if it has to. Same thing for shift+left
.
Anyway, I could probably do that, but what I don't know is how to test if a arrow is pressed while the shift key is held down.
回答1:
Does this handle what you're after?
if (event.shiftKey && event.which == 38) {
//shift was down when up arrow pressed
}
JS Fiddle: http://jsfiddle.net/stroz/hrQEX/3/
来源:https://stackoverflow.com/questions/24502647/arrow-key-pressed-while-shift-key-is-held-down