java 在硬件级别模拟按键 - Windows
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11597892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simulate Key Press at hardware level - Windows
提问by Anti Earth
I'm looking for a language or library to allow me to simulate key strokes at the maximum level possible, without physically pressing the key.
我正在寻找一种语言或库,让我可以在最大程度上模拟击键,而无需实际按下按键。
(My specific measure of the level of the keystroke is whether or not it will produce the same output as a physical Key Press when my computer is already running key listeners (such as MouseKeys and StickyKeys)).
(我对击键级别的具体衡量是当我的计算机已经运行按键侦听器(例如 MouseKeys 和 StickyKeys)时,它是否会产生与物理按键相同的输出)。
I've tried many methods of keystroke emulation;
The java AWT library, Java win32api, python win32com sendKeys, python ctypes Key press, and many more libraries for python and Java, but none of them simulate the key stroke at a close enough level to actual hardware.
(When Windows MouseKeys is active, sending a key stroke of a colon, semi colon or numpad ADD key just produces those characters, where as a physical press performs the MouseKeys click)
我尝试了多种击键模拟方法;java AWT 库、Java win32api、python win32com sendKeys、python ctypes Key press 以及更多用于 Python 和 Java 的库,但它们都没有在足够接近实际硬件的水平上模拟击键。
(当 Windows 鼠标键处于活动状态时,发送冒号、分号或小键盘 ADD 键的击键只会生成这些字符,而物理按下会执行鼠标键单击)
I believe such methods must involve sending the strokes straight to an application, rather than passing them just to the OS.
我相信这些方法必须涉及将笔画直接发送到应用程序,而不是仅将它们传递给操作系统。
I'm coming to the idea that no library for these high (above OS code) level languages will produce anything adequate. I fear I might have to stoop to some kind of BIOS programming.
我开始认为这些高级(高于操作系统代码)级别语言的库不会产生任何足够的东西。我担心我可能不得不屈服于某种 BIOS 编程。
Does anybody have any useful information on the matter whatsoever?
How would I go about emulating key presses in lower level languages?
Should I be looking for a my-hardware-specific solution (some kind of Fujitsu hardware API)?
有没有人有任何关于此事的有用信息?
我将如何用低级语言模拟按键?
我应该寻找特定于我的硬件的解决方案(某种富士通硬件 API)吗?
I almost feel it would be easier to program a robot to simply sit by the hardware and press the keys.
我几乎觉得将机器人编程为简单地坐在硬件旁边并按下按键会更容易。
Thanks!
谢谢!
回答by rfinz
Second solution, super convoluted, a ton of virtualization, diabolical, totally untested, but theoretically should work, unless the datasnip program doesn't actually write to the keyboard buffer, but instead simulates keystrokes like you've been trying to. That would suck, and I would find the description of their product to be highly misleading.
第二种解决方案,超级复杂,大量的虚拟化,恶魔般的,完全未经测试,但理论上应该可以工作,除非 datasnip 程序实际上没有写入键盘缓冲区,而是像您一直在尝试的那样模拟击键。那会很糟糕,我会发现他们产品的描述非常具有误导性。
You'll need:
你需要:
And some knowledge of writing to com ports... which, looks like there's a good python module here: http://pyserial.sourceforge.net/
以及一些写入 com 端口的知识......看起来这里有一个很好的 python 模块:http: //pyserial.sourceforge.net/
First, write a small program that will send characters, hexcode, etc. as necessary to the COM port of your choosing. Second, create a virtual COM port pair using com0com. At this point, connect your program to one of the COM ports created, and datasnip to the other, making sure that both sides of the communication are using identical baud rates, parity and stop bits, and data length parameters.
首先,编写一个小程序,根据需要将字符、十六进制代码等发送到您选择的 COM 端口。其次,使用 com0com 创建一个虚拟 COM 端口对。此时,将您的程序连接到创建的 COM 端口之一,并将 datasnip 连接到另一个,确保通信双方使用相同的波特率、奇偶校验和停止位以及数据长度参数。
At this point you should have a keyboard that is identical to a hardware one, as far as the OS can tell.
就操作系统而言,此时您应该拥有一个与硬件键盘相同的键盘。
回答by Maxim Ky
I used this solution http://oblita.com/interception.htmlIt fully meets my needs (sending keystrokes to direct input game)
我使用了这个解决方案http://oblita.com/interception.html完全满足我的需求(发送按键直接输入游戏)
回答by rfinz
Can you use an MSDN language? They're fairly simple and well documented (google searches are better than the msdn website, for navigation, usually)
你能使用 MSDN 语言吗?它们相当简单且有据可查(谷歌搜索比 msdn 网站更好,通常用于导航)
I found this bit, and I couldn't tell you if it simulates keystrokes at a level close enough to the hardware, but it does appear to be able to generate keystroke events that other programs can recognize, which is what your ultimate goal is.
我发现了这一点,我无法告诉您它是否在足够接近硬件的级别模拟击键,但它似乎确实能够生成其他程序可以识别的击键事件,这就是您的最终目标。
The 'Send' and 'SendWait' Methods seem to be the key ones.
'Send' 和 'SendWait' 方法似乎是关键的方法。
Godspeed.
神速。
回答by Sam King
I'm not on a Windows box to test it against MouseKeys, so no guarantees that it will work, but have you tried AutoHotkey?
我不是在 Windows 机器上用鼠标键测试它,所以不能保证它会工作,但是你试过 AutoHotkey 吗?