Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #39404 > unrolled thread

Re: Read arrow keys

Started byPhil <phil_lor@bigpond.com>
First post2013-02-21 15:04 +1000
Last post2013-02-21 15:04 +1000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Read arrow keys Phil <phil_lor@bigpond.com> - 2013-02-21 15:04 +1000

#39404 — Re: Read arrow keys

FromPhil <phil_lor@bigpond.com>
Date2013-02-21 15:04 +1000
SubjectRe: Read arrow keys
Message-ID<mailman.2146.1361436993.2939.python-list@python.org>
On 21/02/13 13:36, Ned Deily wrote:
> In article <51255C8A.7090302@bigpond.com>, Phil <phil_lor@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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web