dispatchKeyShortcutEvent 在 Chrome OS 环境中未被调用

huangapple go评论56阅读模式
英文:

dispatchKeyShortcutEvent not being called in a Chrome OS environment

问题

我想要实现对常见快捷键如Ctrl + S(保存)、Ctrl + Z(撤销)在Chrome OS环境中做出反应的代码。

我已经在活动中添加了以下代码:

@Override
public boolean dispatchKeyShortcutEvent(KeyEvent event) {
  if (event.getKeyCode() == KeyEvent.KEYCODE_O) {
      openFile(); // Ctrl+O, Shift+O, Alt+O
      return true;
  } else if(event.getKeyCode() == KeyEvent.KEYCODE_Z) {
      if (event.isCtrlPressed()) {
              undoLastAction();
              return true;
      }
  }
  return super.dispatchKeyShortcutEvent(event);
}

我还在活动中添加了Window.Callback

public class ScMain extends AppCompatActivity implements View.OnClickListener,      
        FragmentManager.OnBackStackChangedListener, Window.Callback {

Android Studio显示以下消息:

ComponentActivity.dispatchKeyShortcutEvent只能从具有相同库组前缀的地方调用(引用的groupId=androidx.core带有前缀androidx,从groupId=[我的应用])

尽管代码能够编译而不出错,但dispatchKeyShortcutEvent方法从未被调用。

我无法找到我应该在gradle文件中添加哪个依赖项。或者我是否需要额外的代码?

英文:

I want to implement code to react to common shortcuts like Ctrl + S (save), Ctrl + Z (undo) in a Chrome OS environment.

I have added the following code in the activity:

@Override
public boolean dispatchKeyShortcutEvent(KeyEvent event) {
  if (event.getKeyCode() == KeyEvent.KEYCODE_O) {
      openFile(); // Ctrl+O, Shift+O, Alt+O
      return true;
  } else if(event.getKeyCode() == KeyEvent.KEYCODE_Z) {
      if (event.isCtrlPressed()) {
              undoLastAction();
              return true;
      }
  }
  return super.dispatchKeyShortcutEvent(event);
}

And I have added the Window.Callback to the activity:

public class ScMain extends AppCompatActivity implements View.OnClickListener,      
        FragmentManager.OnBackStackChangedListener, Window.Callback {

Android Studio displays the following message:

ComponentActivity.dispatchKeyShortcutEvent can only be called from within the same library group prefix (referenced groupId=androidx.core with prefix androidx from groupId=[my app])

Although the code compiles without error, the dispatchKeyShortcutEvent method is never called.

I can not find which dependency I should add in my gradle file. Or do I need additional code?

答案1

得分: 0

我通过使用 onKeyShortcut 来解决了这个问题。

@Override
public boolean onKeyShortcut (int keyCode, KeyEvent event) {
    if (event.isCtrlPressed()) {
        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_Z:
                UndoBtnFired(undoBtn);
                break;
            case KeyEvent.KEYCODE_S:
                saveToFile(null);
                break;
            case KEYCODE_L:
                clearDrawnLinks(mContext);
                break;
            default:
                break;
        }
    }

    return true;
}
英文:

I solved this issue by using onKeyShortcut instead

@Override
public boolean onKeyShortcut (int keyCode, KeyEvent event) {
    if (event.isCtrlPressed()) {
        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_Z:
                UndoBtnFired(undoBtn);
                break;
            case KeyEvent.KEYCODE_S:
                saveToFile(null);
                break;
            case KEYCODE_L:
                clearDrawnLinks(mContext);
                break;
            default:
                break;
        }
    }

    return true;
}

huangapple
  • 本文由 发表于 2023年2月9日 00:06:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388550.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定