473,732 Members | 2,219 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Substitute for Win32 GetKeyState

Hi!

Is there a substitute in the .NET Framework for the GetKeyState(... )
function? I want to check whether the shift key is pressed. What's the best
way to do this?
Do I have to use GetKeyState(... ) using PInvoke?

TIA,

Markus
Nov 16 '05 #1
5 5897
Markus,

No. AFAIK there is no managed version of GetKeyState. You need PInvoke for
that.

--

Stoitcho Goutsev (100) [C# MVP]
"Markus Wildgruber" <ms********@sys ka.de> wrote in message
news:ea******** *****@TK2MSFTNG P10.phx.gbl...
Hi!

Is there a substitute in the .NET Framework for the GetKeyState(... )
function? I want to check whether the shift key is pressed. What's the best way to do this?
Do I have to use GetKeyState(... ) using PInvoke?

TIA,

Markus

Nov 16 '05 #2
Hi,

You may handle the KeyDown event of the approprite control (or the form
itself) and check the KeyEventArgs.Sh ift property.

Hope this is what you are looking for.

"Markus Wildgruber" <ms********@sys ka.de> wrote in message
news:ea******** *****@TK2MSFTNG P10.phx.gbl...
Hi!

Is there a substitute in the .NET Framework for the GetKeyState(... )
function? I want to check whether the shift key is pressed. What's the best
way to do this?
Do I have to use GetKeyState(... ) using PInvoke?

TIA,

Markus

Nov 16 '05 #3
Thanks for Shiva's quick response!

Hi Markus,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to get the key state in your
program without invoking GetKeyState function. If there is any
misunderstandin g, please feel free to let me know.

As far as I know, this depends on which event handler your code is in. Some
event handler that has something to do with keys has managed class for you
to check for key state, such as KeyDown, KeyUp event. You can try to
utilize these events to achieve your goal. Or you have to call GetKeyState
API.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #4
Hi!

Thanks for your responses!

Yes, I need to get the state of a key (in particular whether it is pressed
or not), but it has nothing to do with any event handlers. I just want to
check the state at a specific location in code.

To make this clear, here is the problem I am trying to solve: We have an
auto-logon-mechanism in our application. When the user logs on using
Windows-Authetication, he can choose to be automatically logged on the next
time he opens the application. There is a point in code when we read the
options and decide to do the auto-logon. However, by pressing the shift key
when starting the application the user should be able to override the option
and be able to logon using the dialog. At the location in code when we check
whether the auto-logon-option is set we also want to check the state of the
shift key, in order to bypass auto-logon.

In former times we solved problems like this using the
GetKeyState()-API-function. I already solved it using PInvoke but if there's
a pure .NET-way I'd better go with that (in order to use PInvoke only when
there is no other way).

Thanks,

Markus

"Kevin Yu [MSFT]" <v-****@online.mic rosoft.com> schrieb im Newsbeitrag
news:tK******** ********@cpmsft ngxa10.phx.gbl. ..
Thanks for Shiva's quick response!

Hi Markus,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to get the key state in your
program without invoking GetKeyState function. If there is any
misunderstandin g, please feel free to let me know.

As far as I know, this depends on which event handler your code is in. Some event handler that has something to do with keys has managed class for you
to check for key state, such as KeyDown, KeyUp event. You can try to
utilize these events to achieve your goal. Or you have to call GetKeyState
API.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #5
Hi Markus,

If you want to check for Shift, Ctrl or Alt keys there is a static property
in the Control class called Control.Modifie rKeys. You can use that propety
to check for those keys (so called modifier keys). But only for them.

--
HTH
Stoitcho Goutsev (100) [C# MVP]
"Markus Wildgruber" <ms********@sys ka.de> wrote in message
news:uh******** ******@TK2MSFTN GP09.phx.gbl...
Hi!

Thanks for your responses!

Yes, I need to get the state of a key (in particular whether it is pressed
or not), but it has nothing to do with any event handlers. I just want to
check the state at a specific location in code.

To make this clear, here is the problem I am trying to solve: We have an
auto-logon-mechanism in our application. When the user logs on using
Windows-Authetication, he can choose to be automatically logged on the next time he opens the application. There is a point in code when we read the
options and decide to do the auto-logon. However, by pressing the shift key when starting the application the user should be able to override the option and be able to logon using the dialog. At the location in code when we check whether the auto-logon-option is set we also want to check the state of the shift key, in order to bypass auto-logon.

In former times we solved problems like this using the
GetKeyState()-API-function. I already solved it using PInvoke but if there's a pure .NET-way I'd better go with that (in order to use PInvoke only when
there is no other way).

Thanks,

Markus

"Kevin Yu [MSFT]" <v-****@online.mic rosoft.com> schrieb im Newsbeitrag
news:tK******** ********@cpmsft ngxa10.phx.gbl. ..
Thanks for Shiva's quick response!

Hi Markus,

First of all, I would like to confirm my understanding of your issue. From your description, I understand that you need to get the key state in your program without invoking GetKeyState function. If there is any
misunderstandin g, please feel free to let me know.

As far as I know, this depends on which event handler your code is in.

Some
event handler that has something to do with keys has managed class for you to check for key state, such as KeyDown, KeyUp event. You can try to
utilize these events to achieve your goal. Or you have to call GetKeyState API.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."


Nov 16 '05 #6

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

Similar topics

2
16534
by: Jeff Marcum | last post by:
I need to write a query in Access that returns the results of a text field with the spaces replaced with underscores. So, if the data in the field was 'hello world', the query results would show 'hello_world'. The SUBSTITUTE function in excel does this. I can't find an equivalent function in Access. In case this helps the syntax in Excel is SUBSTITUTE(text,old_text,new_text,instance_num) Text is the text or the reference to a cell...
4
2752
by: TC | last post by:
Hey All, Just wondering if there is a .Net substitute to the following Win32 APIs: IsCharAlphaNumeric IsCharAlphaA Regards,
4
18546
by: melanieab | last post by:
Hi, I need to check and see if a Tab key was pressed. I found GetKeyState() but that only seems to be for Visual Basic. Is there anything like it for c#? (GetKeyName didn't seem too promising when I looked over it.) Thanks! Melanie
8
4771
by: Tony Johansson | last post by:
Hello! I just wonder what the difference is between a native win32-app and MFC. What I know is that you can use Win32 API in both cases. I think an MFC application is connected with a GUI which a win32 is not. You may correct me if I'm wrong on this point. //Tony
5
2890
by: Steve Richter | last post by:
I have a public variable ( "mZipServiceUrl" ) in my code behind class that I am able to substitute into the HTML of the .aspx file: <span onclick="WindowOpen('<%=mZipServiceUrl%>')" style="text-decoration:underline;cursor:hand;color:red;"> <%=mZipServiceUrl%> </span> but in the same page, I cant substitute the same variable into a user control:
4
2096
by: vijay.rajamanickam | last post by:
I am a newbie w.r.t. compiler optimizations and I believe these optimizations are generally done in function level. Can I safely assume that if I use getter & setter functions, I can effectively substitute using the qualifier volatile for variables in all instances. Thanks in advance R V
0
1159
by: Edwin Knoppert | last post by:
I would like to embed binary data fragments in a class. in (win32)assembly i used db statements. In older basic there was the data statement. For redistribution purposes i would not like to use a resource (RCDATA) for this. Is there a better solution. I'm using VB in ASP.NET and therefore i can not use \`00 codingstyle (afaik)
1
3176
by: Rocky Zhou | last post by:
I am accustomed to vi my pthon scripts with 'tab' indent. But when I copy some code to my script, the indent may be 'spaces'. So I wanna a way to substitute those 'spaces' to be 'tabs' conveniently. For example, I: expand -t4 test.py >/tmp/test2.py vi /tmp/test2.py Then in vim, the indents should be: /^\( \{4\}\)*/
5
9646
by: ats | last post by:
Hi there, I have the following Function and Const declarations in my project: Public Declare Function GetKeyState Lib "USER32" Alias "GetKeyState" (ByVal nVirtKey As Long) As Integer Public Const VK_SHIFT As Integer = 16 I then have the following code:
0
8774
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9447
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9307
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9235
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 most users, this new feature is actually very convenient. If you want to control the update process,...
1
6735
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.