Get key code as well as key modifiers on key press

我的未来我决定 提交于 2019-12-07 15:55:41

问题


When a user presses a key combination, I need to get key's code and which modifier keys (Ctrl, Alt, Shift etc) are currently pressed, and choose an appropriate reaction based on that. Is there a cleaner way to do this than the following? (using InputProcessor#keyDown):

public boolean keyDown(int keycode) {
      boolean ctrl, alt, shift, ...;
      if (Gdx.input.isKeyPressed(Input.Keys.LEFT_SHIFT) || Gdx.input.isKeyPressed(Input.Keys.RIGHT_SHIFT)) {
            shift = true;
      } else if (/* same for ctrl */) {
          ctrl = true;
      } else  /* same for other modifiers */ {
          //...
      }
      // Finally, choose an action based on the key combination pressed
      if (shift && keycode == Input.Keys.A) {
          // What to do if Shift+A is pressed
      } else if (/* and so on */) {
          //...     
      }
}

I see there are Input.Keys.META_* bitmasks, which are probably what I need, but I haven't found any example on how to use those.


回答1:


You could use the keyDown() method to detect whether the shift (or any other modifier key) is pressed and the keyUp() method whether it is released again:

class MyInputProcesses extends InputProcesses {
  boolean shift = false;
  // etc...

  public boolean keyDown(int keycode) {
    switch(keycode) {
      case Input.Keys.LEFT_SHIFT :
      case Input.Keys.RIGHT_SHIFT:
        shift = true;
        break;
      case Input.Keys.A:
        if(shift) {
          // Do something if Shift+A is pressed
        }
        break;
    }
    // etc...
  }

  public boolean keyUp(int keycode) {
    switch(keycode) {
      case Input.Keys.LEFT_SHIFT :
      case Input.Keys.RIGHT_SHIFT:
        shift = false;
        break;
      // etc...
    }
  }
}

The approach prevents you asking the states of the modifier keys and it is easier to modify the triggers for these keys (e.g. only accept the left shift).

Whether this solution is better/nicer is of personal preference I guess. I doubt it that this will be any (noticeable) faster.




回答2:


you can just iterate over keys and for example add to an array and then you can just check in array whether the proper combination is now used

    //definition of array
    Array<String> buttons = new Array<String>();

    ...
    //now in render

    buttons.clear();

    for(int i = 0; i < 256; i++)
        if( Gdx.input.isKeyPressed( i ) )
            buttons.add( Input.Keys.toString(i) );

    if(buttons.contains("L-Ctrl", false) && buttons.contains("L-Alt", false) && buttons.contains("M", false))
    {
        System.out.println("Ctrl+Alt+M combination entered!");
    }

256 is maximum button index in Input.Keys class (you can go into class and check - also you will find there all String names of buttons)



来源:https://stackoverflow.com/questions/20692838/get-key-code-as-well-as-key-modifiers-on-key-press

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!