pynput icon indicating copy to clipboard operation
pynput copied to clipboard

Distinguish between hardware and software-sent signals

Open H-Theking opened this issue 6 years ago • 4 comments

I didn't find a Google group for the project so I'll ask my question here. I'm using pynput for an input method. It uses character combinations to type a special character in a language. For example to type ə̀, you type eu1 and eu3 to type ə̄.

The algorithm I'm using is simple. I store all the combinations and resulting characters in a dictionary. Each character typed is added to a stack and a new dictionary is created with all the combinations starting with the characters in the stack. The process is repeated until I have a dictionary with one key-value pair. I then send backspace signals and type the resulting character. Let's call this the transformation process.

The issue I'm facing now is that when typing fast, typed characters get caught up during the transformation process and the results are undefined, depending on when your typing gets caught up in the process. For example eu1eu3 is supposed to produce ə̀ə̄, but I got ə̀eū at one time and eəə̄ at some other time.

I have been thinking: if there was a way to distinguish between software and hardware signals, I could handle them independently such that when both signals are sent at the same time, they don't mix.

If anyone has a workaround for my problem or a better approach, I'm open to suggestions.

Cheers Harvey

H-Theking avatar Feb 03 '18 20:02 H-Theking

Hi,

This is currently not possible, since keyboard events sent by a listener do not contain any flags indicating whether they are injected.

It is very possible, however, that this is information provided by all supported platforms; in that case, this should be trivial to add.

On a side note, your method of replacing characters seems a bit fragile. Issue #68 and #70 request a cross-platform way to suppress keyboard events; perhaps this would work better for your use case? This functionality will be added to pynput 1.4, and a flag indicating whether events are injected would be another feature to add to that version.

moses-palmer avatar Feb 05 '18 15:02 moses-palmer

On a side note, your method of replacing characters seems a bit fragile

I'm open to any suggestion for improvement.

Issue #68 and #70 request a cross-platform way to suppress keyboard events; perhaps this would work better for your use case?

I don't think so. I don't wish to suppress events events when characters are being converted. I just want to handle it differently.

and a flag indicating whether events are injected would be another feature to add to that version.

I think this will be most useful to my use case.

H-Theking avatar Feb 05 '18 22:02 H-Theking

I have, by the way, pushed some commits to feature-injected.

As can be seen in commit 1a94ff3, it does not appear to be possible to give an absolute guarantee that events are not in fact injected, and Xorg leaves much to be desired. Windows and OSX do seem to be more reliable.

I you find the time, please test and give feedback on the API!

moses-palmer avatar Feb 26 '18 21:02 moses-palmer

What is the state of this considering the uinput-based backend?

Zireael07 avatar Jun 05 '21 18:06 Zireael07