javascript - Detecting ctrl+z (and other control combos) in paper.js -


i'm trying enable editing commands in paper.js application (such ctrl+z 'undo').

detecting individual letter keys works great, , can detect modifier keys held during mouse events, i'm having trouble writing event handler detects combinations of ctrl , letter keys.

based on the examples given fabric.js, expect key handler looks this:

function onkeydown(event) {     if (event.key == 'z' && event.modifiers.control){        //do thing!     } } 

however, doesn't work! weirdly enough, conditional block never fires. investigate this, wrote following diagnostic handler...

function onkeydown(event) {     console.log(event.key);     console.log(event.modifiers.control); } 

... , tried out various keyboard inputs interesting results:

//ctrl key key: control control: true  //z key key: z control: false  //z key pressed while holding ctrl key:  control: true 

these results suggest string returned event.key different depending on whether control modifier held down when key typed. weird happening here!

based on this, how can detect both of these keys being pressed @ same time?

here couple of vanilla javascript solutions should you:

solution 1

check keycode pressed down , if shiftkey down using native event object.

function handlekeydown(evt) {     if (evt.which === 90 && evt.shiftkey) {         // thing!     } }; 

solution 2

keep global variable detecting if shift key down , use in keydown handler. you'll need reset keyup event handler.

var shiftkeydown = false;  function handlekeydown(evt) {     if (evt.which === 17) {         shiftkeydown = true;     } else if (evt.which === 90 && shiftkeydown) {         // thing!     } };  function handlekeyup(evt) {     if (evt.which === 17) {         shiftkeydown = false;     } }; 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -