上一篇文章中用JNA实现的键盘监听,在Windows上完美运行,能在后台拿到扫描枪扫描输入的内容,可在Linux下还是没办法,于是在网上各种找,又找到两种办法,还是一样,在Windows上运行都没问题,最后都失败在Linux,最后,我看失败的错误描述,貌似问题都出在了图形界面X11上面还有什么键盘布局上,我的Ubuntu系统,不是标准的那种布局,就运行失败了。想了想,虽然失败了,还是记录一下吧,万一有使用redhat等系统的运行没问题呢?
一、使用jnativehook监听键盘输入
jnativehook也是个开源项目,项目地址:,这个项目也是全平台支持的,Windows,Linux、Mac全都支持,但是Linux下貌似是需要装X11,然后键盘布局也得是相关,不然报错了。我运行的报错如下:
报错里面的symbols/pc105我也不懂什么东西,网上一查,感觉就是个键盘类型,就像除了我们用的qwer标准键盘外,还有IBM的有F13的键盘,日文输入键盘等,总之就是我装的Ubuntu系统中没有这个文件,导致报错。但是其他系统说不定就没问题。
这个项目使用也挺简单的,从他的下载JAR包,下载下来,用命令运行java -jar jnativehook.jar,就能出现一个用Swing绘制的图形小程序demo,简单演示监听到的鼠标键盘等输入,具体怎么调用的,将jar包放入eclispe,查看包内的文件就可以看到了。项目要引用,也只需要这一个jar包导入就可以,实现方法也很简单,跟Swing程序中鼠标键盘监听一模一样。下面贴上我简单实现的鼠标监听的代码,其他鼠标监听之类的可以自己研究下官方Demo源码,很明了。
import java.util.logging.Level;import java.util.logging.Logger;import org.jnativehook.GlobalScreen;import org.jnativehook.NativeHookException;import org.jnativehook.dispatcher.DefaultDispatchService;import org.jnativehook.keyboard.NativeKeyEvent;import org.jnativehook.keyboard.NativeKeyListener;public class Test implements NativeKeyListener{ private static final Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName()); public static void main(String[] args) { //关闭日志 logger.setLevel(Level.OFF); new Test(); } public Test() { //添加监听调度员,如果是Swing程序,就用SwingDispatchService,不是就用默认的 GlobalScreen.setEventDispatcher(new DefaultDispatchService()); try { //注册监听 GlobalScreen.registerNativeHook(); //加入键盘监听, GlobalScreen.addNativeKeyListener(this); } catch (NativeHookException e) { e.printStackTrace(); } } //实现键盘监听的三个方法,根据自己情况实现 @Override public void nativeKeyTyped(NativeKeyEvent nativeEvent) { System.out.println("typed"+nativeEvent.paramString()); } @Override public void nativeKeyPressed(NativeKeyEvent nativeEvent) { System.out.println("pressed"+nativeEvent.paramString()); } @Override public void nativeKeyReleased(NativeKeyEvent nativeEvent) { System.out.println("released"+nativeEvent.paramString()); }}
二、使用JXGrabKey来监听
JXGrabKey原本是用来注册热键的,上一篇文章中介绍过,用来实现监听键盘有点怪,但如果没有更好的解决办法时,这也是一种办法,
github项目地址为: 不过这个里面没有相关的jar包和so文件,需要去 这里面下载,需要引入jar包,还需要加载so文件,还用刀了SWT的相关东西,所以你还得加入SWT的jar包,从eclipse目录下找到\plugins\org.eclipse.swt.win32.win32.x86_64_3.106.0.v20170608-0516.jar,这个包,添加到项目中才可以。
具体我的思路是将每一个按键当作一个热键,都去实现自己要实现的操作。虽然很麻烦,但确实管用!而且在我的Ubuntu下运行成功!都没报任何错。可以试试,下面贴上我的代码,只简单实现了将0-9的数字捕获,按下回车即输出。
import java.awt.event.KeyEvent;import java.io.File;import java.util.ArrayList;import java.util.List;import javax.swing.JOptionPane;import jxgrabkey.HotkeyConflictException;import jxgrabkey.HotkeyListener;import jxgrabkey.JXGrabKey;public class JXGrabKeyTest { private static final int MY_HOTKEY_INDEX_0 = 0; private static final int MY_HOTKEY_INDEX_1 = 1; private static final int MY_HOTKEY_INDEX_2 = 2; private static final int MY_HOTKEY_INDEX_3 = 3; private static final int MY_HOTKEY_INDEX_4 = 4; private static final int MY_HOTKEY_INDEX_5 = 5; private static final int MY_HOTKEY_INDEX_6 = 6; private static final int MY_HOTKEY_INDEX_7 = 7; private static final int MY_HOTKEY_INDEX_8 = 8; private static final int MY_HOTKEY_INDEX_9 = 9; private static final int MY_HOTKEY_INDEX_100 = 100; private static boolean hotkeyEventReceived = false; static ListsingleInput = new ArrayList (); private static String caseCode() { StringBuffer buffer = new StringBuffer(); for (Character i : singleInput) { buffer.append(i); } return buffer.toString(); } public static void main(String[] args) throws Exception { //Load JXGrabKey lib System.load(new File("lib/libJXGrabKey.so").getCanonicalPath()); //Enable Debug Output JXGrabKey.setDebugOutput(true); //Register some Hotkey try{ //int key = KeyEvent.VK_K, mask = KeyEvent.CTRL_MASK | KeyEvent.ALT_MASK | KeyEvent.SHIFT_MASK; //int key = KeyEvent.VK_F2, mask = KeyEvent.ALT_MASK; //Conflicts on GNOME JXGrabKey.getInstance().registerAwtHotkey(MY_HOTKEY_INDEX_0, KeyEvent.KEY_LOCATION_UNKNOWN, KeyEvent.VK_0); JXGrabKey.getInstance().registerAwtHotkey(MY_HOTKEY_INDEX_1, KeyEvent.KEY_LOCATION_UNKNOWN, KeyEvent.VK_1); JXGrabKey.getInstance().registerAwtHotkey(MY_HOTKEY_INDEX_2, KeyEvent.KEY_LOCATION_UNKNOWN, KeyEvent.VK_2); JXGrabKey.getInstance().registerAwtHotkey(MY_HOTKEY_INDEX_3, KeyEvent.KEY_LOCATION_UNKNOWN, KeyEvent.VK_3); JXGrabKey.getInstance().registerAwtHotkey(MY_HOTKEY_INDEX_4, KeyEvent.KEY_LOCATION_UNKNOWN, KeyEvent.VK_4); JXGrabKey.getInstance().registerAwtHotkey(MY_HOTKEY_INDEX_5, KeyEvent.KEY_LOCATION_UNKNOWN, KeyEvent.VK_5); JXGrabKey.getInstance().registerAwtHotkey(MY_HOTKEY_INDEX_6, KeyEvent.KEY_LOCATION_UNKNOWN, KeyEvent.VK_6); JXGrabKey.getInstance().registerAwtHotkey(MY_HOTKEY_INDEX_7, KeyEvent.KEY_LOCATION_UNKNOWN, KeyEvent.VK_7); JXGrabKey.getInstance().registerAwtHotkey(MY_HOTKEY_INDEX_8, KeyEvent.KEY_LOCATION_UNKNOWN, KeyEvent.VK_8); JXGrabKey.getInstance().registerAwtHotkey(MY_HOTKEY_INDEX_9, KeyEvent.KEY_LOCATION_UNKNOWN, KeyEvent.VK_9); JXGrabKey.getInstance().registerAwtHotkey(MY_HOTKEY_INDEX_100, KeyEvent.KEY_LOCATION_UNKNOWN, KeyEvent.VK_ENTER); }catch(HotkeyConflictException e){ JOptionPane.showMessageDialog(null, e.getMessage(), e.getClass().getName(), JOptionPane.ERROR_MESSAGE); JXGrabKey.getInstance().cleanUp(); //Automatically unregisters Hotkeys and Listeners //Alternatively, just unregister the key causing this or leave it as it is //the key may not be grabbed at all or may not respond when numlock, capslock or scrollock is on return; } //Implement HotkeyListener HotkeyListener hotkeyListener = new jxgrabkey.HotkeyListener(){ public void onHotkey(int hotkey_idx) { switch(hotkey_idx) { case MY_HOTKEY_INDEX_0: singleInput.add((char) 0); case MY_HOTKEY_INDEX_1: singleInput.add((char) 1); case MY_HOTKEY_INDEX_2: singleInput.add((char) 2); case MY_HOTKEY_INDEX_3: singleInput.add((char) 3); case MY_HOTKEY_INDEX_4: singleInput.add((char) 4); case MY_HOTKEY_INDEX_5: singleInput.add((char) 5); case MY_HOTKEY_INDEX_6: singleInput.add((char) 6); case MY_HOTKEY_INDEX_7: singleInput.add((char) 7); case MY_HOTKEY_INDEX_8: singleInput.add((char) 8); case MY_HOTKEY_INDEX_9: singleInput.add((char) 9); case MY_HOTKEY_INDEX_100: System.out.println(caseCode()); singleInput.clear(); } //hotkeyEventReceived = true; } }; //Add HotkeyListener JXGrabKey.getInstance().addHotkeyListener(hotkeyListener); // //Wait for Hotkey Event// while(!hotkeyEventReceived){// Thread.sleep(1000);// }// // // Shutdown JXGrabKey// JXGrabKey.getInstance().unregisterHotKey(MY_HOTKEY_INDEX); //Optional// JXGrabKey.getInstance().removeHotkeyListener(hotkeyListener); //Optional// JXGrabKey.getInstance().cleanUp(); }}