So I am using an external library for my calculator, here is the link: http://www.openjs.com/scripts/events/keyboard_shortcuts/
And here is the codepen: http://codepen.io/roman_fedoruk/pen/RoRZxd?editors=0011
Basically my issue is that I can’t get my operator shortcuts to work. For example, to add a shortcut with this library you do:
shortcut.add("Ctrl+Shift+X",function() { alert("Hi there!"); });
So I want to add a shortcut for shift+(the equals button)
for the plus operator.
but when I do :
shortcut.add("Shift+=",function()
or:
shortcut.add("Shift+[=]",function()
it doesn’t work.
The documentation says that it supports all special characters but I cannot get it to work… If anyone has any idea of how to do this that would be great!
Maybe my question sounds a bit silly, but are you sure this is a valid key combination? On my German keyboard, Shift+= just does not exist. “=” itself is Shift+0.
On US QWERTY keyboards =
is next to backspace with +
as its shift value. When you press Shift + =
your keyboard drivers / OS handle the built-in shortcut and pass a +
to the browser.
You don’t need an external library (other than, say, jQuery to make coding easier) for that type of shortcut. What you need is to listen to keypresses.
$('#yourCalculator').bind('keypress', function(e) {
console.log(e.keyCode)
});
@cjsheets
Ok, thanks! Didn’t know that was built into javascript, that library was just the first thing I saw when I searched keyboard shortcuts on google…
So I have a question now, when looking at this code:
$('#yourCalculator').bind('keypress', function(e) { console.log(e.keyCode) });
What should I set #yourCalculator to in my code? I cant seem to figure that part out.
It is a valid key combination on my keyboard.
Here is what my keyboard looks like:
As you can see, the plus and equals are on one key right next to the delete button.
1 Like
To use jQuery you’ll need to review how it binds events to elements on the page. In the case of your code you’d probably bind it to either $(body) or $(’.calculator’) if you’re alright with the user needing to select the calculator before input is grabbed.
You could also look to this extension for inspiration https://github.com/jeresig/jquery.hotkeys
Wow thanks! Got all the operator keys working!