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_PRESSED
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 :) 6 7157
"Rune" <ru***@stud.cs.uit.no> a écrit dans le message de
news:6e**************************@posting.google.c om... 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_PRESSED 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
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/ ru***@stud.cs.uit.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_PRESSED 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.GetKeyState().
Thomas
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(event):
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> <ButtonRelease-1>".split():
l.bind(binding, setModifiers)
t.mainloop()
Jeff ru***@stud.cs.uit.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_PRESSED 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(self, self.OnKeyDown)
EVT_KEY_UP(self, self.OnKeyUp)
2. Set or unset a flag on keydown and keyup
def OnKeyDown(self, event):
if (event.GetKeyCode() == WXK_SHIFT):
self.Shift = True
elif (event.GetKeyCode() == WXK_CONTROL):
self.Control = True
def OnKeyUp(self, event):
if (event.GetKeyCode() == WXK_SHIFT):
self.Shift = False
elif (event.GetKeyCode() == WXK_CONTROL):
self.Control = False
Vennlig hilsen, Sean
--
"Hver sin smak", sa vintapperen, han drakk mens de andre sloss.
[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.GetKeyState().
You mean GetAsyncKeyState: "The GetAsyncKeyState 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 GetAsyncKeyState 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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,
|
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...
|
by: shyam |
last post by:
Hi All
I had raised a simillar query in an earlier post
...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| | |