TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Keypress: A Javascript library for capturing input

192 pointsby nsmalchover 11 years ago

21 comments

pyalot2over 11 years ago
Note that you should avoid using this (or anything else with JS) to emulate shortcuts.<p>The reason is because what this usually looks like &quot;oh nice I can define shortcuts people can press&quot;. What it actually means is: &quot;Ohcrap, that shortcut only works for US-US 104 keyboard layouts, and about 80% of people can&#x27;t press that shortcut&quot;.<p>For example: suppose you want a shortcut for shift+&#x2F;, which is for the US keyboard under the small right finger. But that shortcut can&#x27;t be defined for SG (swiss-german) because there the &#x2F; is on the numeric bar where 7 is. So if you assume (for whatever reason) that left+right small finger is a convenient shortcut, and left small finger + right index finger top isn&#x27;t, then you&#x27;re totally screwed. Instead, to reach the right small finger low on swiss german, you&#x27;d have to define shift + &quot;-&quot;. Except that if you define that, it&#x27;ll be one of the most uncomfortable keyboard shortcuts imaginable for US users, which find - on the numeric row top --&gt; bottom left small finger + top right small finger = ugh.<p>The truth is that you cannot do reasonable shortcut definition the way that everybody would like to with JS... yet.<p>But wait, can&#x27;t you like, define some reasonable presets and let people choose their own? The answer is no, because you don&#x27;t have a reliable way to translate a keydown&#x2F;keyup to a unicode char, and the unicode char is related to the modifier keys, not the key cap, so though you can do that shortcut definition, you can&#x27;t show to the user what that shortcut definition actually is, because it&#x27;d be either undefined or completely wrong.<p>I&#x27;ve written at length about this here: <a href="http://codeflow.org/entries/2013/jan/30/keyboard-events-in-javascript-are-broken/" rel="nofollow">http:&#x2F;&#x2F;codeflow.org&#x2F;entries&#x2F;2013&#x2F;jan&#x2F;30&#x2F;keyboard-events-in-j...</a>
评论 #6464960 未加载
评论 #6465191 未加载
评论 #6465629 未加载
评论 #6464842 未加载
评论 #6466248 未加载
Stratoscopeover 11 years ago
This is such nice work that I hesitate to bring up a minor point of coding style... But the use of names_with_underscores looks out of place in JavaScript.<p>JavaScript code traditionally uses PascalCaseNames for constructors and camelCaseNames for variables, properties, and methods. You do see UPPERCASE_NAMES_WITH_UNDERSCORES used sometimes for &quot;constants&quot;, but that&#x27;s about it.<p>Of course, names_with_underscores work just as well as any other names, and some people use them in their own JavaScript code, but I think libraries should follow the traditional native conventions of their language.<p>It would be like releasing a Ruby library that used camelCaseNames for its method names. It would work, but many Ruby developers would feel that those names were a bit out of place in a language where method names customarily use underscores.<p>Mentioning it here basically just as a reminder for other library developers: &quot;When in Rome...&quot;
评论 #6464512 未加载
评论 #6465792 未加载
评论 #6464674 未加载
stephenover 11 years ago
Tangentially, can someone on the W3C please fix the sorry state of key bindings across UAs?<p>Cross-browser idiosyncrasies are finally improving...except for webapp key bindings. Alt-F is taken in IE, Ctrl-whatever is taken in other UAs. Or an extension like (the awesome) vimium conflicts with some key bindings, so you have to disable it all together.<p>To the point where Asana uses <i>tab</i> as their short cut key. Tab! Wtf! &quot;tab-p&quot; for project. Bizarre!<p>But I guess I can&#x27;t blame them, as I wouldn&#x27;t be surprised if it&#x27;s the only shortcut modifier that wasn&#x27;t taken yet across all UAs.<p>I admittedly have no solutions to propose, so am just ranting about an understandably hard problem...but it would be nice if someone came along and fixed this. Please. :-)
评论 #6467101 未加载
评论 #6464209 未加载
dphamover 11 years ago
This looks like <a href="https://github.com/ccampbell/mousetrap" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ccampbell&#x2F;mousetrap</a> I&#x27;m much more fond of the implicit syntax of mousetrap though.
评论 #6470736 未加载
paulirishover 11 years ago
Obligatory link to the fantastic <a href="http://unixpapa.com/js/key.html" rel="nofollow">http:&#x2F;&#x2F;unixpapa.com&#x2F;js&#x2F;key.html</a>
评论 #6465190 未加载
评论 #6464417 未加载
评论 #6465294 未加载
aromanover 11 years ago
Looks great. I was impressed that the keyboard visualization handled up to 7 simultaneous button pressed on my physical keyboard. Honestly, I wasn&#x27;t aware my keyboard&#x27;s wiring (MacBook Air) was capable of handling that.
评论 #6464262 未加载
rcfoxover 11 years ago
Looks neat, but how do you write a keyboard input library with sequence combos and then <i>not</i> have the Konami code as a secret demo?
评论 #6470713 未加载
whisk3rsover 11 years ago
Nice. This seems to solve a common problem for developers building rich UIs.<p>On a similar topic, does anybody have any pointers for implementing keystroke-by-keystroke character replacement in an INPUT box that doesn&#x27;t suck? I&#x27;d like users to be able to enter &quot;townhall&quot; but have the input value be &quot;TOWNHALL&quot; and there doesn&#x27;t seem to be a simple way to do this that doesn&#x27;t fail when the user uses the arrow key or wants to insert more than one character in the middle of an input box.
评论 #6464232 未加载
Tooover 11 years ago
If you use this for hotkeys in your application make sure to return false or set e.handled to true or something like that. I don&#x27;t remember the exact details right now.<p>Otherwise features from the browser like &quot;incremental search&quot; in firefox will get priority over the key captures in your app. In fact <i>both</i> your key capture and the incremental search will trigger which is very frustrating as the page starts scrolling to the first match of the letter you just typed.<p>But be very careful while doing this. Only do it for for your own hotkeys and make sure they are not used for other hotkeys in most common browsers! If you always swallow the keypress, normal hotkeys like ctrl+t for new tab will stop working for the user also.
arnehormannover 11 years ago
I built a demo with some similarities 7 month ago (<i>EDIT</i> - that&#x27;s not a plagiarism charge, the OPs submission predates mine and was created a year ago). <a href="https://gist.github.com/arnehormann/4163916" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;arnehormann&#x2F;4163916</a> for the source and <a href="http://bl.ocks.org/arnehormann/raw/4163916/keycodes.html" rel="nofollow">http:&#x2F;&#x2F;bl.ocks.org&#x2F;arnehormann&#x2F;raw&#x2F;4163916&#x2F;keycodes.html</a> for the demo. The code is documented and includes a konami code and a mode switching example. It&#x27;s set to a German Windows keyboard layout, but the code includes comments on how to change that.
grenover 11 years ago
I know the mess is in the OS-side but is there any chance we will have some day a DOM API independent of the keyboard layout?<p>For instance you want to make a game using WSAD keys, well in a french keyboard it&#x27;s ZSQD... Also in a French keyboard, there some keys which trigger the same keycode so you cannot differenciate them! (I think it was the comma with something else)<p>If you want to make a software like Blender on the web which have a lot of key shortcuts you are a bit screwed!
meemooover 11 years ago
My biggest issue is wanting to bind ctrl&#x2F;meta-x&#x2F;c&#x2F;v to do what you think they would do in my UI (a graph editor), but not do anything in input fields. I can stopPropagation, but it is a pain to keep track of.
评论 #6464662 未加载
ChristianBundyover 11 years ago
This just looks like a development implementation of <a href="http://keycodes.atjayjo.com/" rel="nofollow">http:&#x2F;&#x2F;keycodes.atjayjo.com&#x2F;</a>
osipovasover 11 years ago
I see the potential for reading key presses when people are entering data into a form. I look forwarding to investigating this. Thank you!
评论 #6464640 未加载
taspeotisover 11 years ago
Is it meant to work with all the keys? Chrome 29:<p>&gt; Open page<p>&gt; Don&#x27;t touch anything<p>&gt; Type<p><pre><code> this is a test </code></pre> &gt; Now you&#x27;ve moved down three times.
评论 #6466251 未加载
agumonkeyover 11 years ago
pressing as many keys as possible makes me wonder how is wired my keyboard, I can get all 8 &#x27;asdf&#x27; and &#x27;uiop&#x27; in down state, but not &#x27;asdf&#x27; &#x27;hjkl&#x27; nor &#x27;qwer&#x27; &#x27;hjkl&#x27; .. funny
评论 #6464774 未加载
tommaxwellover 11 years ago
Ooh, I could use this in a project I&#x27;m working on. Thanks!
frozenportover 11 years ago
Pause&#x2F;break not working in Firefox 24.0 on Windows x64 8.
dancecodesover 11 years ago
suggestion: add to console echo when pressed something in your demo
RachelFover 11 years ago
Looks great.
thenerdfilesover 11 years ago
This will do to onscreen keyboards what Wordpress did to onscreen journals.