java windows键,是否可以使用java禁用Windows键

本文介绍了如何使用Java和JNA库来禁用Windows键(Win+Left/Right)以及阻止Alt+Tab切换窗口。通过创建一个Hook,实现在应用程序启动时封锁这些快捷键,在退出时解除封锁。适用于Windows平台的Java开发者。

is it is possible to disable the windows keys and alt+tab using java ...

解决方案

You can use JNA to achieve this.

Include jna.jar and platform.jar on the classpath and create the following class.

This class disables left windows key (0x5B) and right windows key (0x5C). So you can add other codes at the switch case statement.

Call KeyHook.blockWindowsKey() as soon as possible when your application starts.

On the other side, call unblockWindowsKey() at app shutdown.

Since the code is only executed when isWindows() is true, you can always call KeyHook.blockWindowsKey(), even when running on another OS.

import com.sun.jna.platform.win32.Kernel32;

import com.sun.jna.platform.win32.User32;

import com.sun.jna.platform.win32.WinDef.HMODULE;

import com.sun.jna.platform.win32.WinDef.LRESULT;

import com.sun.jna.platform.win32.WinDef.WPARAM;

import com.sun.jna.platform.win32.WinUser.HHOOK;

import com.sun.jna.platform.win32.WinUser.KBDLLHOOKSTRUCT;

import com.sun.jna.platform.win32.WinUser.LowLevelKeyboardProc;

import com.sun.jna.platform.win32.WinUser.MSG;

public class KeyHook {

private static HHOOK hhk;

private static LowLevelKeyboardProc keyboardHook;

private static User32 lib;

public static void blockWindowsKey() {

if (isWindows()) {

new Thread(new Runnable() {

@Override

public void run() {

lib = User32.INSTANCE;

HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);

keyboardHook = new LowLevelKeyboardProc() {

public LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT info) {

if (nCode >= 0) {

switch (info.vkCode){

case 0x5B:

case 0x5C:

return new LRESULT(1);

default: //do nothing

}

}

return lib.CallNextHookEx(hhk, nCode, wParam, info.getPointer());

}

};

hhk = lib.SetWindowsHookEx(13, keyboardHook, hMod, 0);

// This bit never returns from GetMessage

int result;

MSG msg = new MSG();

while ((result = lib.GetMessage(msg, null, 0, 0)) != 0) {

if (result == -1) {

break;

} else {

lib.TranslateMessage(msg);

lib.DispatchMessage(msg);

}

}

lib.UnhookWindowsHookEx(hhk);

}

}).start();

}

}

public static void unblockWindowsKey() {

if (isWindows() && lib != null) {

lib.UnhookWindowsHookEx(hhk);

}

}

public static boolean isWindows(){

String os = System.getProperty("os.name").toLowerCase();

return (os.indexOf( "win" ) >= 0);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值