New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement keyboard and mouse hooks module (say hooks.py) #43
Comments
One more Python module to look at: https://github.com/IronManMark20/hooked |
Another alternative: https://github.com/schurpf/pyhk They are all for keyboard only. We need mouse hooks as well. |
@vasily-v-ryabov, just to clarify. Do you mean mouse/keyboard hooks for processes started/connected to by pywinauto ? The AutoIt example above sets the hook for it's own process. It's a bit different. |
Low-level mouse and keyboard hooks are global. We may set it in operating system and then filter target process events in. I didn't check yet how it works for processes with different privileges. Code signing is another interesting topic that we didn't touch at all. |
Maybe you can put some use cases to better understand the requirements? I could think about hooking of Python interpreter process itself with a running Pywinauto script. (Similar to the specified AutoId example) This could allow to interrupt/pause/whatever the script execution in more easier manner, without trying to the catch interpreter window. However, I still don't see how it can be of use for other processes. |
Currently we have only one example using the outdated pyHook: recorder.py. Since mouse/keyboard hooks are global (i.e. system wide), we can get a window handle from any process (in theory) in the event callback. It means that our Python interpreter instance listens all processes with allowed permission (at least). Let's imagine that you've run pywinauto script with keyboard event handler (say on {F5} key press) that calls a very long chain of actions inside a user software (managed by pywinauto from Python process). It's a kind of a shortcut for automated tasks. The script is working in the background. In that case {F5} can be pressed in a user software window. It looks more convenient than opening Python interpreter console window and pressing {F5} there. |
Looks like LowLevelKeyboarProc/LowLevelMouseProc is the way to go: |
To finish my previous comment:
|
Correct. That's all about Timing restrictions might be more serious for us. I didn't look deeper how it works in AutoIt. Maybe it has the same restrictions. |
On my laptop |
Just wanted to let you know, my hook library does support mouse hooks. I get the mouse position on each callback. You can add hotkeys (hotmice?) By setting a coordinate. It isnt perfect, as you should be able to tell whether it is inside a rectangle, but that just gets down to some math. |
Hi @ironmanmark20, thanks for pointing it out. It's not so obvious looking at the readme example. |
@ironmanmark20, the wiki says |
In a couple hours i can give you what you want. I hide it to make things easier for beginners, but what you want exists in the code. |
Please read the readme with this example. Also, run the other file on any computer with Python 2.7-3.5 or PyPy. How is that? The newest version (0.6) is LGPL, not GPL, so no worries there. |
Hi @ironmanmark20, thanks for you suggestion. I tried to run your example on my Dell laptop and it didn't work straight away. It looks like I get different scan_codes on my machine, they come with huge values. You can refer to attached screenshot with my prints. However I have a more basic question. How do you see your collaboration with pywinauto ? Would you like to make it as a part of the project or you'd prefer to keep it as a separate package? Basically, we try to keep only minimal dependencies for the pywinauto project. |
I would not mind if code was folded straight into the project. I can understand not wanting additional dependencies. It is strange that you scan codes are so high. They are supposed to be pretty standardized, so unless you have a very rare (or old) keyboard (perhaps another language?) you should not be getting those codes. I based my codes on an standard 101 key English keyboard. another possibility would be a different python version or implementation. (I test on CPython, PyPy, IronPython). For example, PyPy returns long values for scan codes for some reason. Anyway, I guess we should test keyboard input automatically. Using some automation framework or something 😜 or perhaps just have a config file. |
Cool. Plugging pyhooked into the project will really simplify the things. Regarding scan_codes, indeed, I have several input languages installed on my Win8.1 and it could be the reason. I tried the script on a VM with Win7 and with English keyboard only and it started printing LCtrl as expected but still didn't get to the foobar print though. Anyway, as for me, your module could be a good start point for the feature. |
@ironmanmark20, thanks for your efforts! We really appreciate it! From what I can see now we probably need to synchronize hotkeys aliases with |
There is interesting cross-platform package: SelfSpy. |
Reviewed the code under the gist. @ironmanmark20 did you notified about the comment? So duplicating it here just in case: Method Magic numbers like It's also not clear why you're using the same variable
|
hooks.py module (implement #43)
Current version is released in pywinauto 0.6.0. So closing this. |
pyHook contains C module. But hooks can be implemented with Python only (using ctypes).
See pyHook example for reference: http://stackoverflow.com/questions/4581772/using-pyhook-to-respond-to-key-combination-not-just-single-keystrokes
One more example of keyboard hook without pyHook: http://stackoverflow.com/questions/9817531/applying-low-level-keyboard-hooks-with-python-and-setwindowshookexa
Analogue in AutoIt:
Thanks TomashUA for the use case.
The text was updated successfully, but these errors were encountered: