[python-win32] getting Windows key codes

Eric S. Johansson esj at harvee.org
Tue Mar 31 01:41:47 CEST 2015



On 3/30/2015 5:50 PM, Tim Roberts wrote:
> Eric S. Johansson wrote:
>> Set with some help for a friend of mine, I was able to implement the
>> system illustrated above. As a result I can mostly successfully use
>> speech recognition on Windows to dictate text into a variety of Linux
>> applications. I discover some problems which make me think that the
>> right solution is to capture windows events/key codes and translate them
>> into the lyrics equivalents. This way I think I can handle many of the
>> special function keys more easily.
> I assume that should be "Linux", rather than "lyrics".

That's one of the ways you can tell someone is using speech recognition. 
If things look weird, just read it out loud and try to listen to what 
you're saying
>
> If you are doing speech recognition, where do Windows events and key
> codes come into play?

It's one of these side effect things. Speech recognition either generate 
straight text or in the case of a matching grammar, perform some action. 
As far as I can tell, NaturallySpeaking shoves events onto the Windows 
input queue. I think there may be two different types because in one 
case they have the general form that usually works but they also have a 
system input stream which is much lower down. I am guessing that they 
translate the normal Latin one character set spoken into Windows events 
much in the same way that I do in my code.

>
>
>> My question is two parts, first is how do I get access to these key
>> codes (equivalent to what's in the Python uinput module). And what kind
>> of framework does Windows demand (text window etc.) in order to be able
>> to expose these key codes.
> Your question isn't clear.  What key codes and events do you want to
> grab?  Perhaps you should give us a couple of specific scenarios that
> describe what you want to have happen.

Good idea. the problem that drove me to the conclusion is 
differentiating between backspace and delete not to mention control H in 
Emacs. There are also things like cut and paste whose key sequences are 
variable depending on the application.

The ASCII character for delete is 7f
it's keycode in uinput/ev.py is KEY_DELETE = (0x01, 111)
the ASCII character for backspace is 08
it's keycode in uinput/ev.py is KEY_BACKSPACE = (0x01, 14)
Lenox equivalent
In a nutshell, what I want to do is given a Windows keycode, translate 
the keycode to the linux equivalent. I'm starting with handling the 
control H problem.

The keycode gives me access to a whole bunch of additional information 
about which characters have been spoken versus the characters one can 
type. For example when I was trying to figure out the difference between 
backspace and delete inside of Emacs, I did the usual ^q to try and trap 
the next character and I always get the escape character. If the key 
codes are what's being used instead of the ASCII character equivalents 
that would explain why the delete key and control H still available for 
help.

If I am able to translate from Windows key codes to the next key codes 
then I will have a less lossy conversion (theoretically and be able to 
handle more of the non-traditional keyboard characters like function 
keys and meta-keys etc.

As for what key codes I need to trap and convert, it's a big I don't 
know. This exploration process is been one big "what the hell were they 
thinking" when I see what Nuance generates in response to what I say.

--- eric


More information about the python-win32 mailing list