【问题标题】:Which Language Should I Use To Code Keys On A Keyboard? [closed]我应该使用哪种语言对键盘上的键进行编码? [关闭]
【发布时间】:2016-10-02 19:38:17
【问题描述】:

如何对键盘上的按键进行编码?我正在尝试制作游戏,我想知道如何为键分配角色。例如:

Spacebar = Splits in two
Q = Macro feed
I = Open/Close Inventory
...

我想我可以使用 python,但我不确定。我的 python 代码是:

n = 2
if(UserPress == Spacebar):
    CellSplits = True
    CellParts = n*2

我认为 jQuery 可能会工作:

$(document).ready(function(){
    var $(Cell) = $('.Cell')
    $('.Cell').mousefollow(Cell)
});

此外,这款游戏将是 Agario 的翻版。

【问题讨论】:

标签: jquery python keyboard key-bindings


【解决方案1】:

jQuery中的基本语法是这样的:

$(document).keypress(function(e) {
  if(e.which == 32) {
    alert('You pressed space');
  }
  if(e.which == 81) {
    alert('You pressed Q');
  }
  if(e.which == 73) {
    alert('You pressed I');
  }
});

或者您可以使用 switch 语句来代替:

$(document).keypress(function(e) {
  switch (e.which) { 
    case 32: 
      alert('You pressed space');
      break;
    case 81:
      alert('You pressed Q');
      break;
    case 73:
      alert('You pressed I');
      break;
  }
});

【讨论】:

  • e.which 是什么意思?它就像分配给什么键一样吗?
  • e 是事件的缩写。你也可以把它写成event.which(但不是event.witch,因为那不是一回事)。 e.which 属性规范了 event.keyCode 和 event.charCode。建议观看 e.which 进行键盘按键输入。这些数字只是 ASCII 字符代码,您可以在很多网站上查找。
猜你喜欢
  • 2011-06-14
  • 1970-01-01
  • 2012-11-06
  • 2016-11-23
  • 2014-10-22
  • 2010-11-03
  • 2010-10-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多