-2

I am working on a PyQt5 based app. My GUI shows 21 buttons: letters from A to Z, I use them as input for

the main algorithm. I would like to be able to input the letters from the keyboard too.

So far following Get printable name of any QKeyEvent key value I am able to achieve this.

Only problem is how to disable keyPressevent , and re-enable it after my app intro screen,

like I do with my letters button doing:

self.groupBox.hide() and re-enabling them after pressing the start new game

button. with self.groupBox.show()

I wrote down in my answer the solution to my problem provided by musicamante. Still I would like to now if is there a way to disable PyQt5 keyboard event processing in a way that my :

def keyPressEvent(self, event):
         print(keyevent_to_string(self, event))
         print('self.letters : ',self.letters)
         print('self.letters_guessed : ',self.letters_guessed )
         if keyevent_to_string(self, event) in self.letters:
                    print('inininininini'*5)
                    self.printo(keyevent_to_string(self, event))

code piece sits there but doesnt produce any result when I press a keyboard key ?

I understood that KeyPressEvent is the handler for QEvent.KeyPress, so is there a way to disable/delete/remove QEvent.KeyPress class in Pyqt5 ?

pippo1980
  • 2,181
  • 3
  • 14
  • 30
  • 2
    Can't you just use a variable to set the "monitoring" status, and then react to key events whether the variable is set or not? Anyway, if you provide us a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) it might help to better understand what you're trying to achieve. – musicamante May 29 '20 at 16:26
  • you better use self.button.setEnable(False) – DaVinci May 29 '20 at 16:26
  • self.button.setEnable(False) , I do that after I press one of the button, I use hide() to hide my keyboard – pippo1980 May 29 '20 at 16:31
  • HI musicamante, thank very much for your suggestion, thats what I did – pippo1980 May 29 '20 at 17:50

1 Answers1

0

following musicamante suggestions I modified my code with:

def keyPressEvent(self, event):
    if self.keyonoff:
         print(keyevent_to_string(self, event))
         print('self.letters : ',self.letters)
         print('self.letters_guessed : ',self.letters_guessed )
         if keyevent_to_string(self, event) in self.letters:
                    print('inininininini'*5)
                    self.printo(keyevent_to_string(self, event))

then switched

self.keyonoff = True and self.keyonoff = False

as needed by my code.

I am getting what I was looking for, not sure if there is another way to do that: 'I understood that KeyPressEvent is the handler for QEvent.KeyPress, so is there a way to disable/delete/remove QEvent.KeyPress class in Pyqt5 ?'

Code below is not mine is just a keyPressEvent reimplamantation usefull to my purpose

from PyQt5.QtCore import Qt

def keyevent_to_string(self, event):
    self.keymap = {}
    for self.key, value in vars(Qt).items():
        if isinstance(value, Qt.Key):
            self.keymap[value] = self.key.partition('_')[2]

    self.modmap = {
        Qt.ControlModifier: self.keymap[Qt.Key_Control],
        Qt.AltModifier: self.keymap[Qt.Key_Alt],
        Qt.ShiftModifier: self.keymap[Qt.Key_Shift],
        Qt.MetaModifier: self.keymap[Qt.Key_Meta],
        Qt.GroupSwitchModifier: self.keymap[Qt.Key_AltGr],
        Qt.KeypadModifier: self.keymap[Qt.Key_NumLock],
        }
    self.sequence = []
    for modifier, text in self.modmap.items():
        if event.modifiers() & modifier:
            self.sequence.append(text)
    self.key = self.keymap.get(event.key(), event.text())
    if self.key not in self.sequence:
        self.sequence.append(self.key)
    return '+'.join(self.sequence)

From Get printable name of any QKeyEvent key value

Edited Edited Edited

More on this hopefully I'll get my right to ask questions back:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri May 29 10:49:58 2020

@author: Pietro
"""

import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtCore import Qt, QEvent

class Window(QtWidgets.QMainWindow):    
    def __init__(self):
        super(Window, self).__init__()
        self.button = QtWidgets.QPushButton("activate", self)
        self.button2 = QtWidgets.QPushButton("deactivate", self)
        self.button.move(0,0)
        self.button2.move(100,100)
        self.button2.clicked.connect(self.deactivated)
        self.button.clicked.connect(self.activated)

    def activated(self):
        print('attivato')
        QWidget.setEnabled(self,True)

    def deactivated(self):
        print('deattivato')
        QWidget.setEnabled(self,False)

    def keyevent_to_string(self, event):
        self.keymap = {}
        for self.key, value in vars(Qt).items():
            if isinstance(value, Qt.Key):
                self.keymap[value] = self.key.partition('_')[2]

        self.modmap = {
            Qt.ControlModifier: self.keymap[Qt.Key_Control],
            Qt.AltModifier: self.keymap[Qt.Key_Alt],
            Qt.ShiftModifier: self.keymap[Qt.Key_Shift],
            Qt.MetaModifier: self.keymap[Qt.Key_Meta],
            Qt.GroupSwitchModifier: self.keymap[Qt.Key_AltGr],
            Qt.KeypadModifier: self.keymap[Qt.Key_NumLock],
            }
        self.sequence = []
        for modifier, text in self.modmap.items():
            if event.modifiers() & modifier:
                self.sequence.append(text)
        self.key = self.keymap.get(event.key(), event.text())
        if self.key not in self.sequence:
            self.sequence.append(self.key)
        return '+'.join(self.sequence)

    def keyPressEvent(self, event):
            print(self.keyevent_to_string(event))

if __name__ == '__main__':

    app = QApplication(sys.argv)
    window = Window()
    window.setGeometry(600, 100, 300, 200)
    window.show()
    sys.exit(app.exec_())

I ended up here

QWidget.setEnabled(False)

apparently disable both keyboard and mouse events. Any idea how to disable just keyboards events ?

pippo1980
  • 2,181
  • 3
  • 14
  • 30