473,394 Members | 1,971 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,394 software developers and data experts.

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 5879
Markus,

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

--

Stoitcho Goutsev (100) [C# MVP]
"Markus Wildgruber" <ms********@syska.de> wrote in message
news:ea*************@TK2MSFTNGP10.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.Shift property.

Hope this is what you are looking for.

"Markus Wildgruber" <ms********@syska.de> wrote in message
news:ea*************@TK2MSFTNGP10.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
misunderstanding, 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.microsoft.com> schrieb im Newsbeitrag
news:tK****************@cpmsftngxa10.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
misunderstanding, 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.ModifierKeys. 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********@syska.de> wrote in message
news:uh**************@TK2MSFTNGP09.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.microsoft.com> schrieb im Newsbeitrag
news:tK****************@cpmsftngxa10.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
misunderstanding, 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
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...
4
by: TC | last post by:
Hey All, Just wondering if there is a .Net substitute to the following Win32 APIs: IsCharAlphaNumeric IsCharAlphaA Regards,
4
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...
8
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...
5
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%>')"...
4
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...
0
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...
1
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'...
5
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
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
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 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.