473,398 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

detect keystate

Hi, I want to detect wether the arrowkeys are pressed down or not. I
would be happy if it would be as simple like "if (keys.Up == true)
something()" but there seems to be harder than this. I would just like
to detect the state, not start some kind of event. Is it possible ?

Jan 8 '07 #1
7 4005
I don't know if there is a managed mechanism for querying random keys
(although Control.ModifierKeys handles Ctrl, Alt, Shift etc); perhaps
need to use P/Invoke?

Untested: (taken from System.Design.NativeMethods)

[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern short GetKeyState(int keyCode);

No idea if this will work, though...

Marc
Jan 8 '07 #2
<wa*********@gmail.comha scritto nel messaggio
Hi, I want to detect wether the arrowkeys are pressed down or not. I
would be happy if it would be as simple like "if (keys.Up == true)
something()" but there seems to be harder than this. I would just like
to detect the state, not start some kind of event. Is it possible ?
If you are on a Control (or form) take a look at the ModifierKeys property.

--
www.neodatatype.net
Jan 8 '07 #3
Actually, ModifierKeys is public static, so you can use
Control.ModifierKeys even if you are writing a console-only
application (give-or-take a reference). Internally it uses the user32
GetKeyState as previously posted (although declared in a separate
class).

Marc
Jan 8 '07 #4
"Marc Gravell" <ma**********@gmail.comha scritto nel messaggio
Actually, ModifierKeys is public static, so you can use
Control.ModifierKeys even if you are writing a console-only application
You're right.
In effects I don't understand why they put the ModifierKeys into
System.Windows.Forms.
I think it should be better into Environment or something.
Jan 8 '07 #5
Probably for the same reason that Cache is in System.Web but can (if
you try) be used by other application types; clearly some areas of the
framework just got placed in the pet assembly of the team developing
it, even if its true use was wider than that scope. I don't know how
things work at MS, but possibly due to avoiding delays in the repo
cascade if placing higher? (pure conjecture here...)

Marc
Jan 8 '07 #6
"Marc Gravell" <ma**********@gmail.comha scritto nel messaggio
Probably for the same reason that Cache is in System.Web but can (if you
try) be used by other application types;
And this is right!
System.Web.Cache rappresent the cache for web application, so it is into
system.Web.
But the keys are not a prerogative of windows applications.
Jan 8 '07 #7
Actually, this *was* the case 1.0/1.1 (which I'll admit is a
*reasonable* reason for this location); in 2.0 you can use it anywhere
a bit more conveniently than the CAB. See KB917411 (note the "More
Information" does not apply in 2.0). In my (limited) opinion this
represents a realisation that this is functionality that it is
desirable to be available at a higher level.

Marc
Jan 8 '07 #8

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

Similar topics

8
by: R. Rajesh Jeba Anbiah | last post by:
Here is a nice code to detect utf-8 <http://in2.php.net/utf8_encode#39986> But, I couldn't find out the logic behind the script. If anyone knows that please share. Particularly I would like to...
23
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...
23
by: Michel Bany | last post by:
I am trying to parse responseXML from an HTTP request. var doc = request.responseXML; var elements = doc.getElementsByTagName("*"); the last statement returns an empty collection when running from...
10
by: Ben Xia | last post by:
Is there some way can detect MAC address with PHP? any help will be appreciate. Ben
2
by: Paul Ledger | last post by:
I want to be able to display a context menu when the user righthand drags an item into a textbox. It states in the documentation that the e.KeyState property will be set accordingly in the drop...
6
by: hb | last post by:
Hi, Would you please tell me how to detect if the client's browser is closed? I need such event to trigger a database modification. Thank you hb
2
by: Nathan | last post by:
I am doing a drag-drop in a treeview. In the DragOver and DragDrop event handlers, I check the KeyState (e.KeyState) to see if CTRL is pressed. When I hold the CTRL key down, e.KeyState in the...
4
by: goscottie | last post by:
I used submodal as my popup window. With some tweaks, it working great in my app. However, I can't find a way to detect session timeout in the popup window. The app is a form based...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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,...
0
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...

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.