Java后台获取USB⼆维码扫描内容(Java监听系统键盘操作)
上⼀篇⽂章中⽤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(Package().getName());
public static void main(String[] args) {
//关闭⽇志
logger.setLevel(Level.OFF);
new Test();
}
public Test() {
//添加监听调度员,如果是Swing程序,就⽤SwingDispatchService,不是就⽤默认的
GlobalScreen.setEventDispatcher(new DefaultDispatchService());
try {
//注册监听
//加⼊键盘监听,
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) {
jfinal jar包下载System.out.println("released"+nativeEvent.paramString());
}
}
⼆、使⽤JXGrabKey来监听
JXGrabKey原本是⽤来注册热键的,上⼀篇⽂章中介绍过,⽤来实现监听键盘有点怪,但如果没有更好的解决办法时,这也是⼀种办法,
github项⽬地址为:不过这个⾥⾯没有相关的jar包和so⽂件,需要去这⾥⾯下载,需要引⼊jar包,还需要加载so⽂件,还⽤⼑了SWT的相关东西,所以你还得加⼊SWT的jar包,从eclipse⽬录下到\lipse.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 List<Character> singleInput = new ArrayList<Character>();
private static String caseCode() {
StringBuffer buffer = new StringBuffer();
for (Character i : singleInput) {
buffer.append(i);
}
String();
}
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
n e){
JOptionPane.showMessageDialog(null, e.getMessage(), e.getClass().getName(), JOptionPane.ERROR_MESSAGE);
//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
////Wait for Hotkey Event
// while(!hotkeyEventReceived){
// Thread.sleep(1000);
// }
/
/
//// Shutdown JXGrabKey
// Instance().unregisterHotKey(MY_HOTKEY_INDEX); //Optional
// Instance().removeHotkeyListener(hotkeyListener); //Optional
// Instance().cleanUp();
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论