全局keyDown / keyPress EmberJS中的事件,而无需设置焦点 [英] Global keyDown/keyPress events in EmberJS without having to set focus

查看:132
本文介绍了全局keyDown / keyPress EmberJS中的事件,而无需设置焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经有一个jQuery应用程序,一旦文档准备就有一个'keydown'事件。基本上,任何时候用户点击一个键,它将匹配键码并做某事。现在我将这个应用程序移植到了Ember,生活变得困难,原因有二:

I used to have a jQuery app, that had a 'keydown' event once the document was ready. Basically, anytime a user hit a key, it would match the keycode and do something. Now I ported this app to Ember and life became difficult for 2 reasons:

1)唯一可以注册'keyDown'的事件是当包含元素/视图专注于它。通过设置tabindex值可以实现非输入字段的唯一方法是实现这一点,我这样做:

1) The only time a 'keyDown' event can be registered is when the containing element/View has a focus on it. The only way this can be done with non-input fields is by setting the tabindex value, to achieve this, I do this:

didInsertElement:function(){
            this.$().attr("tabindex",0);
            this.$().focus(); //bringing focus to the view in order to ensure keyDown event is triggered :)
     },

    keyDown:function(event){
            var keycode = event.which || event.keycode;
            switch(keycode){
                   //perform various events
            }
    }

2)所以上面的工作就像一个魅力,除了它在应用程序上创建一个丑陋的突出重点,每当应用程序失去焦点时,keyDown事件不会触发。我需要明确点击ApplicationView区域(这不是整个身体)。

2) So the above works like a charm, except, it creates an ugly highlighted focus on the application and whenever the application loses focus, the keyDown events do not trigger. I need to explicitly click around the ApplicationView area (which is not the entire body).

我真正想要的是一个简单的keydown事件触发与当我在jQuery中这样做。任何身体都有想法是什么?

All I really want is to have a simple keydown event triggered the same way it was when I had this in jQuery. Any body got ideas what's up?

推荐答案

Ember专门只捕获在ember应用程序范围内发生的事件,所以如果你想抓住你的应用程序(因为您的整个页面不是一个ember应用程序),而不必专注于ember,您将不得不在ember范围之外创建这些事件。

Ember specifically only catches events that happen within the scope of the ember app, so if you want to catch events external to your app (since your entire page isn't an ember app), and not have to have focus on ember you'll have to create those events outside of the scope of ember.

这篇关于全局keyDown / keyPress EmberJS中的事件,而无需设置焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆