Read arrow keys

Phil phil_lor at bigpond.com
Thu Feb 21 00:04:19 EST 2013


On 21/02/13 13:36, Ned Deily wrote:
> In article <51255C8A.7090302 at bigpond.com>, Phil <phil_lor at bigpond.com>
> wrote:
>> I'm attempting to put a simple remote control program together and to do
>> so I need to check if the arrows are pressed. I could probably do this
>> with pyqt4 but I'm looking for something simple to experiment with.
>>

<cut>

Thanks for your reply Ned.

> You don't say which o/s platform(s) you are interested in but, if they
> are all unix-y and you don't mind running from a shell terminal session,
> you could just use the venerable curses module in the Python standard
> library.

Whatever I manage to cobble together will only be used under Linux.

I had a look at the curses module and it seems that it will detect key 
presses but I don't know if it will detect two arrow presses at the same 
time, which is what I need.

I haven't had much success with pygame so I have put something together 
with pyqt4 which almost works.

     def keyPressEvent(self, event):
          self.firstrelease = True
          #astr = "pressed: " + str(event.key())
          self.keylist.append(event.key())

# the following if and elif statements only detects single key presses
#         key = event.key()
#         if key == QtCore.Qt.Key_Left:
#           print "left"

#         elif key == QtCore.Qt.Key_Up:
#           print "up"

#         elif key == QtCore.Qt.Key_Up and key == QtCore.Qt.Key_Left :
#           print "left and up"

     def keyReleaseEvent(self, event):
          if self.firstrelease == True:
              self.processmultikeys(self.keylist)

              self.firstrelease = False

              del self.keylist[-1]

     def processmultikeys(self,keyspressed):
         print keyspressed


I'm not sure how to handle "keyspressed". For example, "keyspressed" 
returns 16777235 for "up" and returns 16777234 for "left". What logic do 
I need to detect when "up" and "left" are both pressed?

Another problem. When "up" and "left" are pressed "keyspessed" returns 
16777235 and 16777234. From then on "keyspressed" always returns 
16777235 and 16777234 even after a single key is pressed. It seems to me 
that "keylist" needs to be emptied. My experiments in this area have 
also not yielded anything useful.

-- 
Regards,
Phil



More information about the Python-list mailing list