I'm writing an SWT application which needs to sit in the system tray and pop up automatically whenever the user connects some USB device (the application serves as its control panel).
我正在编写一个SWT应用程序,它需要放在系统托盘中,并在用户连接某个USB设备时自动弹出(该应用程序用作其控制面板)。
The way to do this in the native environment (win32 in this case, but I should be platform-independent ultimately) is to listen to the WM_DEVICECHANGE event, then checking if my device has been disconnected.
在本机环境中执行此操作的方法(在这种情况下为win32,但我最终应该与平台无关)是监听WM_DEVICECHANGE事件,然后检查我的设备是否已断开连接。
Googling the subject, it seems like SWT does not in fact handle this type of event. Does anyone have any idea as to how to achieve this? My fallback solution would be sampling the USB port every n seconds, looking for the device, but this is a no-no solution as far as I'm concerned...
谷歌搜索主题,似乎SWT实际上并没有处理这种类型的事件。有没有人知道如何实现这一目标?我的后备解决方案是每隔n秒对USB端口进行采样,寻找设备,但就我而言,这是一个禁止解决的问题......
Thanks and cheers
谢谢和欢呼
Shai
4 个解决方案
#1
EDIT: J-16 SDiZ reported that the API exists for Windows too
编辑:J-16 SDiZ报告说,该API也适用于Windows
Here is a very good article about Access USB devices from Java applications
这是一篇关于从Java应用程序访问USB设备的非常好的文章
The described jUSB module contains a USBListener object.
所描述的jUSB模块包含USBListener对象。
Interface implemented by objects that want to monitor USB structure. The order in which these changes are reported is not necessarily going to be the order in which the changes were seen in the real world, and delays also occur.
由想要监视USB结构的对象实现的接口。报告这些变化的顺序不一定是现实世界中变化的顺序,也会发生延迟。
Youre SWt object can implement this listener and do something, when the method
你的SWt对象可以实现这个监听器并在方法时做一些事情
deviceAdded(Device dev)
is fired
#2
Java does not provide direct interaction with the devices and with USB. There are several third-party libraries for Java that provide USB-related features (read/write from USB port). For example: http://www.icaste.com/ (commercial)
Java不提供与设备和USB的直接交互。有几个用于Java的第三方库提供与USB相关的功能(从USB端口读/写)。例如:http://www.icaste.com/(商业)
#3
Your comment for Zorglub suggests that you have some (presumably) JNI/JNA code to call the 3rd party DLL you mentioned. I assume you could also write a JNI wrapper to the Windows API where you can register to be notified for the _WM_DEVICECHANGE_ event you mentioned.
您对Zorglub的评论表明您有一些(大概)JNI / JNA代码来调用您提到的第三方DLL。我假设您还可以在Windows API中编写JNI包装器,您可以在其中注册以获得有关您提到的_WM_DEVICECHANGE_事件的通知。
#4
There is a jUSB port for windows at http://www.steelbrothers.ch/jusb/
在http://www.steelbrothers.ch/jusb/上有一个用于窗户的jUSB端口
#1
EDIT: J-16 SDiZ reported that the API exists for Windows too
编辑:J-16 SDiZ报告说,该API也适用于Windows
Here is a very good article about Access USB devices from Java applications
这是一篇关于从Java应用程序访问USB设备的非常好的文章
The described jUSB module contains a USBListener object.
所描述的jUSB模块包含USBListener对象。
Interface implemented by objects that want to monitor USB structure. The order in which these changes are reported is not necessarily going to be the order in which the changes were seen in the real world, and delays also occur.
由想要监视USB结构的对象实现的接口。报告这些变化的顺序不一定是现实世界中变化的顺序,也会发生延迟。
Youre SWt object can implement this listener and do something, when the method
你的SWt对象可以实现这个监听器并在方法时做一些事情
deviceAdded(Device dev)
is fired
#2
Java does not provide direct interaction with the devices and with USB. There are several third-party libraries for Java that provide USB-related features (read/write from USB port). For example: http://www.icaste.com/ (commercial)
Java不提供与设备和USB的直接交互。有几个用于Java的第三方库提供与USB相关的功能(从USB端口读/写)。例如:http://www.icaste.com/(商业)
#3
Your comment for Zorglub suggests that you have some (presumably) JNI/JNA code to call the 3rd party DLL you mentioned. I assume you could also write a JNI wrapper to the Windows API where you can register to be notified for the _WM_DEVICECHANGE_ event you mentioned.
您对Zorglub的评论表明您有一些(大概)JNI / JNA代码来调用您提到的第三方DLL。我假设您还可以在Windows API中编写JNI包装器,您可以在其中注册以获得有关您提到的_WM_DEVICECHANGE_事件的通知。
#4
There is a jUSB port for windows at http://www.steelbrothers.ch/jusb/
在http://www.steelbrothers.ch/jusb/上有一个用于窗户的jUSB端口