PDA

View Full Version : Detecting keyboard input from any window



illuzioner1
14th March 2020, 21:18
Is there a way I can monitor keyboard input if the Qt window is not active? In addition to regular alphanumeric keys, I want to passively monitor function keys and control characters.

I prefer Python, but I'll do it in C++ if I need to. I used pynput to capture keys in PyQt, but it has issues identifying some control keys (like ctrl-. or ctrl-,) which I need.

I know Qt has built in capturing for its active windows but what about across all windows (without interfering with the intended action of those keys)?

d_stranz
14th March 2020, 21:57
If a window does not have the input focus then I don't think it will receive any keyboard events, and I am not sure how a window can have input focus without being active. In any case if you want to capture events from all windows, install an event filter on your application instance. In your filter, be sure to return the correct value or pass the event along to the base class.

illuzioner1
18th March 2020, 12:48
Thank you for the response. It looks like pynput can monitor keys even when it doesn't have focus. There are some buried values in its callbacks (something called vk) that help identify some keys, but I had to back it out to map from vk # to actual key.

JFYI

Thanks!

d_stranz
18th March 2020, 14:37
Then pynput is probably monitoring the keyboard at a much lower level than Qt. By the time keystrokes get to the Qt GUI level, they have already been filtered through the window system.