Java 中是否可以发出小键盘箭头键?

发布于 2025-01-16 14:47:31 字数 1137 浏览 0 评论 0原文

背景 操作系统(我使用的是 Windows 10,如果重要的话)有一个名为 鼠标键 的功能允许您使用数字键盘执行鼠标操作。按 5 单击,按 8/2/4/6 上/下/左/右移动光标,依此类推。

欲望 我想在 Java 中以编程方式激活鼠标键时移动光标。我特别想为此使用键盘事件,因为在我所处的上下文中直接移动鼠标是被禁止的。

问题 我尝试将 Robot.keyPressRobot.keyRelease 与键码 KeyEvent.VK_KP_UP = 224 一起使用,但这会产生以下错误:

java.lang.IllegalArgumentException: Invalid key code 
    at java.desktop/sun.awt.windows.WRobotPeer.keyPress(Native Method)
    at java.desktop/java.awt.Robot.keyPress(Robot.java:350)

并尝试使用KeyEvent.VK_NUMPAD8 不会出错,但也不会使鼠标移动。

我还尝试了jnativehook的GlobalScreen.postNativeEvent(new NativeKeyEvent(...)),再多的巫术也无法让它发挥作用。 NativeKeyEvent 中没有键盘向上键的常量,因此这并不奇怪。当我尝试发送密钥代码 224 时,出现以下错误:

com.github.kwhat.jnativehook.GlobalScreen postNativeEvent
WARNING: map_keyboard_event [97]: Unable to lookup scancode: 224

我是否在问不可能的事情?

Background
Operating systems (I'm on Windows 10, if it matters) have a feature called Mouse Keys that allow you to perform the operations of the mouse using the numpad. Press 5 to click, 8/2/4/6 to move the cursor up/down/left/right, and so on.

The Desire
I would like to move the cursor around when Mouse Keys is activated, programmatically, in Java. I specifically want to use keyboard events for this, as moving the mouse directly is verboten in the context I'm in.

The Problem
I tried using Robot.keyPress and Robot.keyRelease with keycode KeyEvent.VK_KP_UP = 224, but that yields the following error:

java.lang.IllegalArgumentException: Invalid key code 
    at java.desktop/sun.awt.windows.WRobotPeer.keyPress(Native Method)
    at java.desktop/java.awt.Robot.keyPress(Robot.java:350)

And trying it with KeyEvent.VK_NUMPAD8 doesn't error out, but also doesn't make the mouse move.

I also tried jnativehook's GlobalScreen.postNativeEvent(new NativeKeyEvent(...)), and no amount of voodoo could get it to work either. There's no constant in NativeKeyEvent for the keypad up key, so that's not super surprising. When I try to send the keycode 224, the following error occurs:

com.github.kwhat.jnativehook.GlobalScreen postNativeEvent
WARNING: map_keyboard_event [97]: Unable to lookup scancode: 224

Am I asking the impossible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文