Question

ANSWER: There isn't a natively managed equivalent for this method. However, a good example managed code API can be found over at pinvoke.net.

Hi all

I have an application that accepts a flag for 'trace mode'. Trace mode turns on a form containing a text box. The text box displays output via a custom TraceListener object. If there is a -t present on the command-line, the trace window will be opened from within its own low-priority thread. It's handy for consultants that are trying to diagnose the application when something goes wrong.

A few consultants have requested that pressing some obscure key combination during startup should turn on trace mode so they don't have to go to the command line to activate it every time. It's a reasonable request.

I don't want to give them a button or menu item for this because a lot of the critical information that the consultants will care about is displayed to Trace at startup before the main form is created as the various components are instantiated and initialized.

I thought it should have been simple enough to detect the current keyboard state in .NET - but it seems that everywhere I look, the advice is to use [DllImport("user32.dll", EntryPoint = "GetKeyboardState", SetLastError = true)] as the method to detect what's going on.

That's all well and good - I'm comfortable with that. However, as a general rule of thumb I try to make sure I use native .NET managed code for Windows functions before I go trying to roll my own.

Is there a native .NET method or equivalent to user32.dll's GetKeyboardState?

Was it helpful?

Solution

Control.ModiferKeys is a static property of Control which, if any, modifier keys are currently pressed. I'm not aware of anything to get general keyboard state, seems like most code P/Invoke with either GetKeyboardState or GetKeyState for checking specific keys.

OTHER TIPS

Are you sure that PreviewKeyDownEventArgs does not have what you need? If it does, then you can use the PreviewKeyDown event.

Another option might be accelerator keys. A Windows API function can use an accelerator table in the application's resources to map keyboard sequences to commands, where a command is the same thing that a menu item does; you know, a WM_COMMAND message. I know it is easy to do in MFC using C++ but when I tried to find the C# way to do it, I got lost. If you can figure out how to do accelerator keys using C# then it could be a flexible solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top