Checking if pressed key is an alphabet or space in PyQt

Viewed 908

I have implemented a custom QTextEdit class in which I am overiding the keyPressEvent method.

I want to check if the key that was pressed is among the following:
1. Capital alphabets
2. Lowercase alphabets
3. Space

If my function declaration is something like:

def keyPressEvent(self, event):

I could do something like:

if event.key() in (
    QtCore.Qt.Key_A,
    QtCore.Qt.Key_B,
    QtCore.Qt.Key_C,
    QtCore.Qt.Key_D,
    ....
    ....

And so on.

How would I loop through these values? And how will I access the actual character that is pressed? I want to create a string of the keys pressed as long as they are alphabets.

3 Answers

Found an answer. event.key() is just a hexadecimal number.

Okay @Jonas has a good answer and depending on what you trying to do you do not have to ever loop through a list you can simple check to see if it is IN a list;

myList = [Qt.Key_A,Qt.Key_B,Qt.Key_C,... Qt.Key_Z]

if event.text() in myList:
   print("In My List")
Related