473,574 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to detect that a key is being pressed, not HAS been pressed earlier!??

Hey

I'm trying to build a gui application and need to know if the user is
actually holding down the shift or ctrl key. That is, if the user
currently is holding down the shift key. In pseudo code this will boil
down to something like this:

def test:
if user presses shift:
return SHIFT_IS_PRESSE D
elif user presses ctrl:
return CTRL_IS_PRESSED
else
return false

It's important to notice here that I'm not interested if the user has
already pressed shift or ctrl. I'm only interested in knowing if he is
currently holding down one of these keys. (I have looked into msvcrt
and the like but have found no answer..) The function should also work
in both windows and Linux.

Any help is appriciated :)
Jul 18 '05 #1
6 7176
"Rune" <ru***@stud.cs. uit.no> a écrit dans le message de
news:6e******** *************** ***@posting.goo gle.com...
Hey

I'm trying to build a gui application and need to know if the user is
actually holding down the shift or ctrl key. That is, if the user
currently is holding down the shift key. In pseudo code this will boil
down to something like this:

def test:
if user presses shift:
return SHIFT_IS_PRESSE D
elif user presses ctrl:
return CTRL_IS_PRESSED
else
return false

It's important to notice here that I'm not interested if the user has
already pressed shift or ctrl. I'm only interested in knowing if he is
currently holding down one of these keys. (I have looked into msvcrt
and the like but have found no answer..) The function should also work
in both windows and Linux.

Any help is appriciated :)


You should have a look at the wxPython demo (wxKeyEvents section) that shows
how this is handled in a cross platform fashion.

Search for wxPython from www.sf.net

--
Gilles
Jul 18 '05 #2
Rune <ru***@stud.cs. uit.no> pisze:
It's important to notice here that I'm not interested if the user has
already pressed shift or ctrl. I'm only interested in knowing if he is
currently holding down one of these keys. (I have looked into msvcrt
and the like but have found no answer..) The function should also work
in both windows and Linux.


This event *is* available in WinAPI, but don't ask me where. I'm sure,
because VCL (which is built on top of WinAPI and uses standard Windows
messages) distinguishes KeyUp and KeyDown events.

If you want to have it available also in linux, use Qt/PyQt.

--
Jarek Zgoda
Unregistered Linux User #-1
http://www.zgoda.biz/ JID:zgoda-a-chrome.pl http://zgoda.jogger.pl/
Jul 18 '05 #3
ru***@stud.cs.u it.no (Rune) writes:
Hey

I'm trying to build a gui application and need to know if the user is
actually holding down the shift or ctrl key. That is, if the user
currently is holding down the shift key. In pseudo code this will boil
down to something like this:

def test:
if user presses shift:
return SHIFT_IS_PRESSE D
elif user presses ctrl:
return CTRL_IS_PRESSED
else
return false

It's important to notice here that I'm not interested if the user has
already pressed shift or ctrl. I'm only interested in knowing if he is
currently holding down one of these keys. (I have looked into msvcrt
and the like but have found no answer..) The function should also work
in both windows and Linux.


No idea about linux, but on windows you use win32api.GetKey State().

Thomas
Jul 18 '05 #4
In Tk, each event has a 'state' field, which can tell you whether
modifier keys or mouse buttons were pressed when an event was received.
Note that for the KeyPress event which is a modifier key, the state
field will not reflect the newly pressed key (the same goes for button
presses), as implied by this section of the XKeyEvent manpage:

The state member is set to indicate the logical state of the pointer
buttons and modifier keys just prior to the event, which is the bitwise
inclusive OR of one or more of the button or modifier key masks: But-
ton1Mask, Button2Mask, Button3Mask, Button4Mask, Button5Mask, Shift-
Mask, LockMask, ControlMask, Mod1Mask, Mod2Mask, Mod3Mask, Mod4Mask,
and Mod5Mask.

from Tkinter import *

# for pre-2.3 versions
#def enumerate(l):
# for i in range(len(l)): yield i, l[i]

# Set up a mapping from bits to names
# (I'm not sure if these values match on Windows)
names = "Shift Caps Control Alt Num Mod3 Mod4 Mod5 1 2 3 4 5".split()
mods = [(1<<i, v) for i, v in enumerate(names )]

# Here's a handler that gets various events and looks at the state field
def setModifiers(ev ent):
s = []
for i, v in mods:
if event.state & i: s.append(v)
if not s: s = ['(none)']
var.set(" ".join(s))

# Set up a minimal user interface
t = Tk()
var = StringVar(t)
l = Label(t, textv = var, width=32)
l.pack()

# including a couple of bindings (the text will update when
# one of these bindings is triggered)
for binding in "<Motion> <1> <ButtonReleas e-1>".split():
l.bind(binding, setModifiers)
t.mainloop()

Jeff

Jul 18 '05 #5
ru***@stud.cs.u it.no (Rune) writes:
Hey

I'm trying to build a gui application and need to know if the user is
actually holding down the shift or ctrl key. That is, if the user
currently is holding down the shift key. In pseudo code this will boil
down to something like this:

def test:
if user presses shift:
return SHIFT_IS_PRESSE D
elif user presses ctrl:
return CTRL_IS_PRESSED
else
return false

It's important to notice here that I'm not interested if the user has
already pressed shift or ctrl. I'm only interested in knowing if he is
currently holding down one of these keys. (I have looked into msvcrt
and the like but have found no answer..) The function should also work
in both windows and Linux.

Any help is appriciated :)


In wxPython you could do it like this ...

1. Bind the key press events to the two functions

EVT_KEY_DOWN(se lf, self.OnKeyDown)
EVT_KEY_UP(self , self.OnKeyUp)

2. Set or unset a flag on keydown and keyup

def OnKeyDown(self, event):
if (event.GetKeyCo de() == WXK_SHIFT):
self.Shift = True
elif (event.GetKeyCo de() == WXK_CONTROL):
self.Control = True

def OnKeyUp(self, event):
if (event.GetKeyCo de() == WXK_SHIFT):
self.Shift = False
elif (event.GetKeyCo de() == WXK_CONTROL):
self.Control = False

Vennlig hilsen, Sean
--
"Hver sin smak", sa vintapperen, han drakk mens de andre sloss.
Jul 18 '05 #6

[Rune]
It's important to notice here that I'm not interested if the user has
already pressed shift or ctrl. I'm only interested in knowing if he is
currently holding down one of these keys.
[Thomas] No idea about linux, but on windows you use win32api.GetKey State().


You mean GetAsyncKeyStat e: "The GetAsyncKeyStat e function determines
whether a key is up or down at the time the function is called"

Not GetKeyState: "The key status returned from this function changes as a
thread reads key messages from its message queue. The status does not
reflect the interrupt-level state associated with the hardware."

....assuming, Rune, that you *really do* want the current state. If your
program is event driven, and you're responding to Shift+Click or Ctrl+Drag
or similar, then you want GetKeyState. Imagine the machine is running
very slowly. I Shift+Click your app and release the Shift key before your
app responds. You call GetAsyncKeyStat e and see that Shift is not
pressed. Had you used GetKeyState, it would tell you that the Shift key
was pressed at the time of the click.

--
Richie Hindle
ri****@entrian. com
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
20430
by: Scott Shaw | last post by:
Hi all, I was wondering if you could help out with this problem that I am having. What I am trying to do is detect keyboard input in a while loop without halting/pausing the loop until the key is pressed (without hitting return). I looked at serveral faq's on the net and installed the cspan readkey module and neither seems to work most likey...
23
6492
by: David McCulloch | last post by:
QUESTION-1: How can I detect if Norton Internet Security is blocking pop-ups? QUESTION-2a: How could I know if a particular JavaScript function has been declared? QUESTION-2b: How could I know if Window.Open has been redefined? BACKGROUND:
3
7940
by: Tegdeep | last post by:
Here's what I want to do: I have a hash table which contains data associated to different keys. The Hash keys are represented by a single character from the keyboard: 0-9, a-z, A-Z, and the other special characters. I want my program to detect what key the user pressed from the keyboard and get the associated data from the hash table...
2
21218
by: Prabhudhas Peter | last post by:
How to Detect the "TAB" Key is pressed in the Text Box or Combo Box.... Which events catches the TAB key... I Want to perform some operations when the "TAB" key is pressed... -- Peter...
0
1059
by: TheSteph | last post by:
Hi, In Windows Forms : How can I detect that a Shortcut (I mean the Shortcut enum) has been pressed ? Thanks,
2
5037
by: Justin | last post by:
I'm creating a kiosk type application and this would be too simple for someone to figure out: Private Declare Function GetAsyncKeyState Lib "user32" ( ByVal vKey As Short) As Short Const VK_ESCAPE As Short = &H1B How can I upgrade this to detect multiple keystrokes? For example CRTL + E Any points in the right direction would be much...
6
2045
by: shyam | last post by:
Hi All I had raised a simillar query in an earlier post http://groups.google.com/group/comp.lang.c++/browse_thread/thread/f371af248d90ead6/1d054627402539e1?lnk=gst&q=ofstream+write+failure&rnum=1#1d054627402539e1 Basically I have a file which is opened through ofstream for writing. Now I want to check if the file has been deleted or...
24
9279
Mague
by: Mague | last post by:
Hey, Im a 13 yr old who needs help. I need a code which can detect if Ctrl R has been pressed. I cannot use e.key thingy because i have got text box on mi page so that doesnt work. I also need it to beable to be pressed and detected when my file is minimized If someone could help i would apressate it ty Mague
4
6458
by: raylopez99 | last post by:
Compound question: first, and this is not easy, if there's a way to detect multiple simultaneous key presses in C# let me know (in the below code, keys c and d being pressed simultaneously or nearly so). I researched this and for C# (as opposed to MFC) there is no library function, and no easy way, though some code on the net suggested that...
0
7835
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8265
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7847
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6500
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5645
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3787
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2265
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1365
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1096
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.