[PyQt] how to set one putton press per shortcut key?

Steve Castellotti sc at puzzlebox.info
Fri Jul 30 01:07:03 BST 2010


Hello all-


     I have a set of buttons in my application which I want the user to 
be able to operate via mouse or keyboard. If they click the mouse 
pointer on the button, the button presses and stays down for as long as 
they hold down the mouse button. When they let go the button pops back up.

     However, if they press and hold the associated keyboard key, the 
button presses twice - once quickly, then again (staying down until they 
let go of the key).

     Despite much experimentation and reading I can't understand why 
this is happening. I want the button press to function with the keyboard 
exactly they way it does with the mouse.


     Here's how I'm attaching the keyboard shortcut signal to the button:

action = QtGui.QAction(self)
action.setShortcut(QtGui.QKeySequence("W"))
self.connect(action, QtCore.SIGNAL("activated()"), 
self.pushButtonForward, QtCore.SLOT("animateClick()"))
self.addAction(action)


     I have tried to make sure AutoRepeat is not set:

self.pushButtonForward.setAutoRepeat(False)
self.pushButtonForward.setAutoRepeatDelay(0)
self.pushButtonForward.setAutoRepeatInterval(0)


     ...but this has no effect.


     If I turn on Auto Repeat:

self.pushButtonForward.setAutoRepeat(True)


     ...then the button presses like crazy for as long as I hold down 
the key (as expected).


     If I try messing with the action:

action.setAutoRepeat(False)


     ...then when I press the key, the first, fast keypress happens, 
then the second, longer hold-down-as-long-as-I-hold-the-key-down never 
happens, which is the opposite of what I want (I want to remove the 
first keypress and just have the second one).


     Can anyone please offer suggestions as to what I might be doing wrong?


     Looking at the "animateClick()" signal, that defaults to 100 ms (so 
perhaps is causing the problem), but I can't seem to override it in that 
statement by putting "animateClick(0)" in there - maybe I'm just confused?



Cheers


Steve Castellotti




More information about the PyQt mailing list