Substitute for Win32 GetKeyState

  • Thread starter Thread starter Markus Wildgruber
  • Start date Start date
M

Markus Wildgruber

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

No. AFAIK there is no managed version of GetKeyState. You need PInvoke for
that.
 
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.

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
 
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."
 
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
 
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.
 
Back
Top