转载自互联网,自己修改了一部分。JAVA实现屏蔽非winlogon的系统快捷键。
使用到的jar包:
jna-4.2.2.jar
jna-platform-4.2.2.jar
使用到的exe程序:
KeyboardLock.exe(第一篇文章生成的exe)
从config.properties配置键值对:
//是否打开chrome的全屏APP模式,值为true或者其它
launch=true
//打开该链接,可任意设置
url=http://www.baidu.com
//我电脑上chrome的安装路径,替换成自己的,注意\转译符。
chromePath=C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
KeyboardHook.java文件,按包名路径
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.keyboardhook.hook;
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;
public class KeyboardHook implements Runnable {
private WinUser.HHOOK hhk;
// 钩子回调函数
private WinUser.LowLevelKeyboardProc keyboardProc = new WinUser.LowLevelKeyboardProc() {
@Override
public LRESULT callback(int nCode, WPARAM wParam,
WinUser.KBDLLHOOKSTRUCT event) {
if (nCode >= 0) {
System.out
.println("nCode:" + wParam + ", KEY: " + event.vkCode);
// 屏蔽windows,alt,tab,del键
switch (event.vkCode) {
case 49198:// Ctrl+Alt+Del
case 24603:// Ctrl+Shift+Esc
case 5:// Win+L
case 27:// esc
case 91: // 左 win
case 92: // 右 win
case 162:// 左ctl
case 163:// 右ctl
case 164:// 左alt
case 165:// 右ctl
case 9:// tab
case 122:// f11
case 123:// f12
case 46:// del
return new LRESULT(1);
}
}
return User32.INSTANCE.CallNextHookEx(hhk, nCode, wParam, null);
}
};
public void run() {
System.out.println("Hook On!");
HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);
hhk = User32.INSTANCE.SetWindowsHookEx(User32.WH_KEYBOARD_LL,
keyboardProc, hMod, 0);
int result;
WinUser.MSG msg = new WinUser.MSG();
while ((result = User32.INSTANCE.GetMessage(msg, null, 0, 0)) != 0) {
System.out.println(System.currentTimeMillis() + "!");
if (result == -1) {
} else {
User32.INSTANCE.TranslateMessage(msg);
User32.INSTANCE.DispatchMessage(msg);
}
}
}
}
BrowserLaunch.java文件,按包名路径
package com.keyboardhook.launch;
import java.lang.reflect.Method;
public class BrowserLaunch {
public static void openURL(String url,String localBrowsePath) {
try {
browse(url,localBrowsePath);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void browse(String url,String localBrowsePath) throws Exception {
//获取操作系统的名字
String osName = System.getProperty("os.name", "");
if (osName.startsWith("Mac OS")) {
//苹果的打开方式
Class fileMgr = Class.forName("com.apple.eio.FileManager");
Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] { String.class });
openURL.invoke(null, new Object[] { url });
} else if (osName.startsWith("Windows")) {
//windows的打开方式。
Runtime.getRuntime().exec(localBrowsePath +" --kiosk " + url);
} else {
// Unix or Linux的打开方式
String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" };
String browser = null;
for (int count = 0; count < browsers.length && browser == null; count++)
//执行代码,在brower有值后跳出,
//这里是如果进程创建成功了,==0是表示正常结束。
if (Runtime.getRuntime().exec(new String[] { "which", browsers[count] }).waitFor() == 0)
browser = browsers[count];
if (browser == null)
throw new Exception("Could not find web browser");
else
//这个值在上面已经成功的得到了一个进程。
Runtime.getRuntime().exec(new String[] { browser, url });
}
}
}
KeyboardHookApp.java文件,按包名路径
package com.keyboardhook.main;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Properties;
import com.keyboardhook.hook.KeyboardHook;
import com.keyboardhook.launch.BrowserLaunch;
public class KeyboardHookApp {
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
// 使用InPutStream流读取properties文件
BufferedReader bufferedReader = new BufferedReader(new FileReader(
"config.properties"));
properties.load(bufferedReader);
// 获取key对应的value值
String launch = properties.getProperty("launch").trim();
String url = properties.getProperty("url").trim();
String chromePath = properties.getProperty("chromePath").trim();
String keyboardLock=properties.getProperty("keyboardLock").trim();
// 调用exe禁止winlogon
Runtime rn = Runtime.getRuntime();
Process p = null;
try {
p = rn.exec(keyboardLock);
System.out.println("KeyboardHook Success!");
} catch (Exception e) {
System.out.println("Error KeyboardHook!");
}
//打开浏览器
if ("true".equals(launch)) {
BrowserLaunch.openURL(url, chromePath);
}
// 禁用alt、ctrl、shift
new KeyboardHook().run();
}
}
config.properties配置文件,放在项目根目录
launch=true
keyboardLock=KeyboardLock.exe
url=http://www.baidu.com
chromePath=C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe