0

I am trying to block all keys on the keyboard except for the caps locks key using pyHook. Below is code that blocks only the a and A key, I am looking to do the opposite. What do I need to do to reverse this?

I have tried to put all keys on keyboard beside caps but that seems like the wrong way to approach it

def OnKeyboardEvent(event):
# block only the letter A, lower and uppercase
return (event.Ascii not in (ord('a'), ord('A')))

Robbysav
  • 1
  • 1

2 Answers2

0

It seems that if the function returns True the event is not blocked.

import string
def OnKeyboardEvent(event):
    return event.Ascii in string.ascii_uppercase

Or perhaps return.Ascii in map(ord, string.ascii_uppercase).

Dan D.
  • 73,243
  • 15
  • 104
  • 123
  • This seems to work extremely well, except for the fact that its not detecting that as an ascii key. Looks like CAPS is not designated in Ascii so I do not know how I will go about getting this information. – Robbysav Feb 02 '19 at 07:43
0

As caps lock itself is not printable, it is not reflected in the Ascii-Attribute of your event.

But there is other attributes - namely the KeyID:

http://pyhook.sourceforge.net/doc_1.5.0/

Use this to look for caps lock, according to this table:

https://learn.microsoft.com/en-us/windows/desktop/inputdev/virtual-key-codes

deets
  • 6,285
  • 29
  • 28