英文:
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论