Quadratics:替换鼠标点击的键盘笔划(Quadratics: Replace Keyboard stroke for Mouse clicks)

我不是用Javascript进阶的。 我希望有人能够简单地解释编辑以下代码的过程。

this.hideNextButton();
this.hidePreviousButton();

var that = this;

Event.observe(document, 'keydown', function keydownCallback(e) {
  var choiceID = null;

  switch (e.keyCode) {
    case 74: // 'j' was pressed
      choiceID = 1;
      break;
    case 75: // 'k' was pressed
      choiceID = 2;
      break;
  }

  if (choiceID) {
    Event.stopObserving(document, 'keydown', keydownCallback);
    that.setChoiceValue(choiceID, true);
    that.clickNextButton();
  }
}); 
  
 

如果用户想要单击键盘上的一个字母来替换鼠标的点击,例如。 点击下一个问题的J或选择是,点击A.我认为这就是代码正在做的事情,但我想分开一点点来添加或删除字母来完成额外的任务,如下一个问题等。

任何帮助或指向正确的方向是一种帮助!

I am not advanced with Javascript. i was hoping for someone to simply explain the process to edit the following code.

this.hideNextButton();
this.hidePreviousButton();

var that = this;

Event.observe(document, 'keydown', function keydownCallback(e) {
  var choiceID = null;

  switch (e.keyCode) {
    case 74: // 'j' was pressed
      choiceID = 1;
      break;
    case 75: // 'k' was pressed
      choiceID = 2;
      break;
  }

  if (choiceID) {
    Event.stopObserving(document, 'keydown', keydownCallback);
    that.setChoiceValue(choiceID, true);
    that.clickNextButton();
  }
}); 
  
 

If a user wants to replace the click of a mouse with clicking a letter on the Keyboard, eg. Click J for the next question or to select yes, click A. I think that is what this code is doing but I'd like to pull it apart a bit to add or remove letters to complete additional tasks, such as next question etc.

Any help or pointing in the right direction is a help!

最满意答案

在您提供的代码中,按'j'或'k'可以通过设置选项值来回答当前问题,并转到下一页。 要添加其他键盘按键,您可以使用适当的键码将其他情况添加到开关。 例如,如果你想让'j'进入下一页,'a'来回答'Yes',那就是这样(删除if(choiceID)部分):

Event.observe(document, 'keydown', function keydownCallback(e) { switch (e.keyCode) { case 65: // 'a' was pressed that.setChoiceValue(1, true); break; case 74: // 'j' was pressed Event.stopObserving(document, 'keydown', keydownCallback); that.clickNextButton(); } });

In the code you provided pressing 'j' or 'k' answers the current question by setting the choice value and goes to the next page. To add other keyboard presses you would additional cases to the switch using the appropriate keycode. For example, if you wanted 'j' to just go to the next page and 'a' to answer 'Yes', it would be something like this (remove the if(choiceID) section):

Event.observe(document, 'keydown', function keydownCallback(e) { switch (e.keyCode) { case 65: // 'a' was pressed that.setChoiceValue(1, true); break; case 74: // 'j' was pressed Event.stopObserving(document, 'keydown', keydownCallback); that.clickNextButton(); } });

更多推荐