Is there any way to catch key combination ctrl+x+return in jquery(or javascript), such that if user presses this key combination, a function is called. I tried using the jquery hotkeys plugin, but that didn’t worked.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use a global boolean array
var keys = []to check whether a key is pressed. Then use the following function to add a global hotkey:As you can see it adds a new listener to the
'keydown'event. This listener will first set the corresponding value inkeystrue and then execute a test, whether the givenkeyValuesare currently true. Note that you cannot removekeys[e.keyCode] = trueand put it in another listener because this could result in a wrong callback order (first hotkey testing, then key mapping). TheexecuteHotkeyTestitself is very easy too:At last you have to add another listener to
keyupto clean the released keys fromkeys.Now you can add a hotkey to ctrl+x+enter by using
addGlobalHotkey(callback,[13,17,88]):JSFiddle demo
Instead of adding a listener for every hotkey you can use a global
[[callback1,values1],[callback2,values2],...]array.Important note: in IE prior version 9 you have to use
attachEventinstead ofaddEventListener. Since you’re already using jQuery you could use.on(...)or.keydowninstead.