阻止 Win+R 打开 运行 工具
Stop Win+R from opening run tool
在我的 javafx 程序中有一个弹出窗口,它允许用户按键,然后相应地设置标签。我的问题是组合键是底层 OS 的快捷方式,例如,如果用户按 Win+R 然后 Run.exe 启动,但我的程序应该只将标签设置为 "Win+R"。我的问题是如何阻止按键事件触发 OS 快捷方式。
这里是相关代码。
public void showInput() {
Set codes = new HashSet();
Stage inputWindow = new Stage();
GridPane pane = new GridPane();
Scene scene = new Scene(pane);
Label label = new Label("Here comes the pressed keys");
scene.setOnKeyPressed(e -> {
e.consume();
int code = e.getCode().ordinal();
if (label.getText().equals("Here comes the pressed keys")){
codes.add(code);
label.setText(String.valueOf(e.getCode().getName()));
} else if (!codes.contains(code)){
codes.add(code);
label.setText(label.getText() + "+" + e.getCode().getName());
}
});
scene.setOnKeyReleased(e -> {
e.consume();
inputWindow.close();
});
pane.add(label, 0, 0);
inputWindow.setScene(scene);
inputWindow.show();
}
我尝试了 e.consume()
但没有用。
不可能,Java 层在 OS 层之上,这意味着您的代码由 JVM 处理,而 JVM 由 OS 处理。所以没有办法 "skip" OS 层并将你的命令直接发送到 Java。
JNA 可以实现,但 坏主意。不要拦截众所周知的组合键。
尽管如此,下面是一个工作示例。它基本上使用 SetWindowsHookEx
Win32 API 然后在挂钩回调中阻止 Win+R 组合键。
import com.sun.jna.platform.win32.*;
public class Test {
public static User32.HHOOK hHook;
public static User32.LowLevelKeyboardProc lpfn;
public static void main(String[] args) throws Exception {
WinDef.HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);
lpfn = new User32.LowLevelKeyboardProc() {
boolean winKey = false;
public WinDef.LRESULT callback(int nCode, WinDef.WPARAM wParam, WinUser.KBDLLHOOKSTRUCT lParam) {
if (lParam.vkCode == 0x5B)
winKey = (lParam.flags & 0x80) == 0;
if (lParam.flags == 0 && lParam.vkCode == 0x52 && winKey) {
System.out.println("Win-R pressed");
return new WinDef.LRESULT(-1);
}
return User32.INSTANCE.CallNextHookEx(hHook, nCode, wParam, lParam.getPointer());
}
};
hHook = User32.INSTANCE.SetWindowsHookEx(User32.WH_KEYBOARD_LL, lpfn, hMod, 0);
if (hHook == null) {
System.out.println("Unable to set hook");
return;
}
User32.MSG msg = new User32.MSG();
while (User32.INSTANCE.GetMessage(msg, null, 0, 0) != 0) {
}
if (User32.INSTANCE.UnhookWindowsHookEx(hHook))
System.out.println("Unhooked");
}
}
(所需的 JNA JAR 依赖项是 net.java.dev.jna : platform)
在我的 javafx 程序中有一个弹出窗口,它允许用户按键,然后相应地设置标签。我的问题是组合键是底层 OS 的快捷方式,例如,如果用户按 Win+R 然后 Run.exe 启动,但我的程序应该只将标签设置为 "Win+R"。我的问题是如何阻止按键事件触发 OS 快捷方式。
这里是相关代码。
public void showInput() {
Set codes = new HashSet();
Stage inputWindow = new Stage();
GridPane pane = new GridPane();
Scene scene = new Scene(pane);
Label label = new Label("Here comes the pressed keys");
scene.setOnKeyPressed(e -> {
e.consume();
int code = e.getCode().ordinal();
if (label.getText().equals("Here comes the pressed keys")){
codes.add(code);
label.setText(String.valueOf(e.getCode().getName()));
} else if (!codes.contains(code)){
codes.add(code);
label.setText(label.getText() + "+" + e.getCode().getName());
}
});
scene.setOnKeyReleased(e -> {
e.consume();
inputWindow.close();
});
pane.add(label, 0, 0);
inputWindow.setScene(scene);
inputWindow.show();
}
我尝试了 e.consume()
但没有用。
不可能,Java 层在 OS 层之上,这意味着您的代码由 JVM 处理,而 JVM 由 OS 处理。所以没有办法 "skip" OS 层并将你的命令直接发送到 Java。
JNA 可以实现,但 坏主意。不要拦截众所周知的组合键。
尽管如此,下面是一个工作示例。它基本上使用 SetWindowsHookEx
Win32 API 然后在挂钩回调中阻止 Win+R 组合键。
import com.sun.jna.platform.win32.*;
public class Test {
public static User32.HHOOK hHook;
public static User32.LowLevelKeyboardProc lpfn;
public static void main(String[] args) throws Exception {
WinDef.HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);
lpfn = new User32.LowLevelKeyboardProc() {
boolean winKey = false;
public WinDef.LRESULT callback(int nCode, WinDef.WPARAM wParam, WinUser.KBDLLHOOKSTRUCT lParam) {
if (lParam.vkCode == 0x5B)
winKey = (lParam.flags & 0x80) == 0;
if (lParam.flags == 0 && lParam.vkCode == 0x52 && winKey) {
System.out.println("Win-R pressed");
return new WinDef.LRESULT(-1);
}
return User32.INSTANCE.CallNextHookEx(hHook, nCode, wParam, lParam.getPointer());
}
};
hHook = User32.INSTANCE.SetWindowsHookEx(User32.WH_KEYBOARD_LL, lpfn, hMod, 0);
if (hHook == null) {
System.out.println("Unable to set hook");
return;
}
User32.MSG msg = new User32.MSG();
while (User32.INSTANCE.GetMessage(msg, null, 0, 0) != 0) {
}
if (User32.INSTANCE.UnhookWindowsHookEx(hHook))
System.out.println("Unhooked");
}
}
(所需的 JNA JAR 依赖项是 net.java.dev.jna : platform)