In my application I have a panel which contains some settings information. This panel is opened/closed with a button click, but I'd also like to be able to close it by hitting esc
on the keyboard.
The code for my View looks like this:
Social.MainPanelView = Ember.View.extend({
elementId: 'panel-account-settings',
classNames: ['panel', 'closed'],
keypress: function(e){
console.log(e);
console.log('keypress');
},
close: function(){
this.$().prepareTransition().addClass("closed");
}
});
I've tried keyup and keydown as well but nothing happens. I suspect that it's because that this isn't an "input" type View but just a standard view. So how can I trigger a method on a View from a key event?
I should clarify that this is not within the context of a Route for this particular element. I'm opening the panel standalone as can be seen in this video:
http://screencast.com/t/tDlyMud7Yb7e
UPDATE 1
Here's a quick fiddle that I've created to illustrate the issue I'm having. I can get the click event to trigger quite easily, but I'm looking for a page wide keyup event that will detect the esc key being pressed and trigger a method on a specific View:
For this, you have to import HostListener in your component ts file. import { HostListener } from '@angular/core'; then use below function anywhere in your component ts file. @HostListener('document:keyup', ['$event']) handleDeleteKeyboardEvent(event: KeyboardEvent) { if(event.
The simple solution: just use the keyup event (and not the keydown event). This will give you the latest value after the user typed the latest character. That resolves the issue.
keydown – fires when any key is pressed down, fires first, and always before the browser processes the key (e.g. inserting text, moving focus, etc). keypress – fires when a key that produces a character value is pressed down, fires after keydown , and before the browser processes the key.
To detect only whether the user has pressed a key, use the onkeydown event instead, because it works for all keys.
For keyPress to work, you have to bring focus to the view. When it is an input, it works because you are bringing focus to it.
Something like this:
App = Em.Application.create();
App.IndexController = Em.Controller.extend({
updateKey: function(keyCode) {
// Do what you gotta do
this.set('shortcutKey', keyCode);
},
shortcutKey: null
});
App.IndexView = Em.View.extend({
didInsertElement: function() {
return this.$().attr({ tabindex: 1 }), this.$().focus();
},
keyDown: function(e) {
this.get('controller').send('updateKey', e.keyCode);
}
});
Here are a working example: http://jsbin.com/ihuzov/1
I think you have misspelled the keypress event. It should be keyPress. For sake of completeness, the following are the events that can be handled by a View (taken from Ember source/doc):
Possible events names for any of the responding approaches described above are:
Touch events:
touchStart
touchMove
touchEnd
touchCancel
Keyboard events
keyDown
keyUp
keyPress
Mouse events
mouseDown
mouseUp
contextMenu
click
doubleClick
mouseMove
focusIn
focusOut
mouseEnter
mouseLeave
Form events:
submit
change
focusIn
focusOut
input
HTML5 drag and drop events:
dragStart
drag
dragEnter
dragLeave
drop
dragEnd
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With