2、屏蔽非winlogon控制的win快捷键,用java+vb生成的exe实现

转载自互联网,自己修改了一部分。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
Features Automatic mapping from Java to native functions, with simple mappings for all primitive data types Runs on most platforms which support Java Automatic conversion between C and Java strings, with customizable encoding/decoding Structure and Union arguments/return values, by reference and by value Function Pointers, (callbacks from native code to Java) as arguments and/or members of a struct Auto-generated Java proxies for native function pointers By-reference (pointer-to-type) arguments Java array and NIO Buffer arguments (primitive types and pointers) as pointer-to-buffer Nested structures and arrays Wide (wchar_t-based) strings Native long support (32- or 64-bit as appropriate) Demo applications/examples Supported on 1.4 or later JVMs, including JavaME (earlier VMs may work with stubbed NIO support) Customizable marshalling/unmarshalling (argument and return value conversions) Customizable mapping from Java method to native function name, and customizable invocation to simulate C preprocessor function macros Support for automatic Windows ASCII/UNICODE function mappings Varargs support Type-safety for native pointers VM crash protection (optional) Optimized direct mapping for high-performance applications. COM support for early and late binding. COM/Typelib java code generator. Community and Support All questions should be posted to the jna-users Google group. Issues can be submitted here on Github. When posting to the mailing list, please include the following: What OS/CPU/architecture you're using (e.g. Windows 7 64-bit) Reference to your native interface definitions (i.e. C headers), if available The JNA mapping you're trying to use VM crash logs, if any Example native usage, and your attempted Java usage It's nearly impossible to indicate proper Java usage when there's no native reference to work from. For commercial support, please contact twalljava [at] java [dot] net. Using the Library Getting Started Functional Description. Mapping between Java and Native Using Pointers and Arrays Using Structures and Unions Using By-Reference Arguments Customization of Type Mapping Callbacks/Function Pointers/Closures Dynamically Typed Languages (JRuby/Jython) Platform Library Direct Method Mapping (Optimization) Frequently Asked Questions (FAQ) Avoiding Crashes Primary Documentation (JavaDoc) The definitive JNA reference is in the JavaDoc. Developers Contributing to JNA Setting up a Windows Development Environment Setting up an Android Development Environment Setting up a RaspberryPi Development Environment Setting up a Mac Development Environment Releasing JNA Publishing to Maven Central Contributing You're encouraged to contribute to JNA. Fork the code from https://github.com/java-native-access/jna and submit pull requests. For more information on setting up a development environment see Contributing to JNA. If you are interested in paid support, feel free to say so on the jna-users mailing list. Most simple questions will be answered on the list, but more complicated work, new features or target platforms can be negotiated with any of the JNA developers (this is how several of JNA's features came into being). You may even encounter other users with the same need and be able to cost share the new development. License This library is licensed under the LGPL, version 2.1 or later, and (from version 4.0 onward) the Apache Software License, version 2.0. Commercial license arrangements are negotiable. NOTE: Oracle is not sponsoring this project, even though the package name (com.sun.jna) might imply otherwise.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值