检测Backbone中的多个按键

时间:2013-04-17 20:19:53

标签: backbone.js

我想抓住像 Alt + g 这样的电子按键事件,我的骨干代码现在是这样的:

class MyView extends Backbone.View
  template: ...

  events:
    "keydown input" : "logKey"

  logKey: (e) ->
    if e.keyCode == 18 and e.keyCode == 71
      console.log('did it!')

哪个不起作用,因为一次只有一个事件键码。

如何实现这一目标?谢谢!

2 个答案:

答案 0 :(得分:0)

为什么不寻找keypress而不是keydown?像这样:

class MyView extends Backbone.View
  events:
    "keypress input" : "logKey"

  logKey: (e) ->
    console.log('did it') if(e.keyCode == 169)

演示:http://jsfiddle.net/ambiguous/fmr8D/

答案 1 :(得分:-1)

您必须设置一个单独的变量来跟踪修饰键并在您的keydown事件中检查它。像:

events:
    "keydown input" : "logKeyDown",
    "keyup input" : "logKeyUp"

logKey: (e) ->
    if e.keyCode == 18
         altDown = true

    if altDown && e.keyCode == 71
      console.log('did it!')

logKeyUp: (e) ->
    if e.keyCode == 18
        altDown = false