Skip to content

Instantly share code, notes, and snippets.

@locks
Last active April 9, 2019 11:25
Show Gist options
  • Save locks/21b06487899d1ba235b6 to your computer and use it in GitHub Desktop.
Save locks/21b06487899d1ba235b6 to your computer and use it in GitHub Desktop.
Detecting keypress (SO)
import Ember from 'ember';
export default Ember.Controller.extend({
appName:'Ember Twiddle'
});
<style>
html, body {
width:100%;
height:100%;
margin:0;
padding:0;
}
.app-body {
width:100%;
height:100%;
background:#212121;
color:#eee;
padding:20px;
box-sizing:border-box;
}
*:focus {
outline: 4px solid tomato;
}
</style>
<h1>Welcome to {{appName}}</h1>
<p>Answer to <a href="http://stackoverflow.com/questions/34958499/detecting-keypress-in-ember-js-v2" target="_blank">Detecting keypress in Ember.js 2.x</a></p>
<br>
<br>
{{outlet}}
{{my-component item=this setAction="updateKey" }}
<br>
<br>
import Ember from 'ember';
export default Ember.Component.extend({
didRender: function() {
this.$().attr({ tabindex: 1 });
this.$().focus();
},
shortcutKey: null,
keyDown(event) {
this.set('shortcutKey', String.fromCharCode(event.keyCode));
}
});
<div class="app-body">
Pressed: {{shortcutKey}}
</div>
{
"version": "0.5.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.2.0/ember.debug.js",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.2.0/ember-data.js",
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.2.0/ember-template-compiler.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment