I'd like to retrieve the state of the Ctrl
keys in a place where I don't have a form.
Normally to get a key state I'd use Control_KeyDown / KeyUp events. How开发者_JAVA技巧ever, the code that needs to know whether Ctrl is pressed is outside any form. There is a form displayed, but the code is supposed not to depend on that form but finding the key state on its own.
Surely there is a way to do that, only I don't succeed to find it on google.
Note that although the code doesn't "have" a form available, its still a WinForms application, so maybe the framework provides some class/object for me to achieve that goal.
Background:
During the application startup phase, I want one step to behave differntly if the Ctrl key is being pressed in that moment. The startup phase displays a splash screen, but the code for the startup isn't aware of that. Instead it reports progress to a callback, and that callback updates the splash screen.
If I use the splash screen for fetching the KeyDown event, I make the startup code depend on that splash screen, which introduces a circular dependency. I want to keep the freedom to remove the splash screen and replace by something different.
You could use the static method on Control
called ModifierKeys
Control.ModifierKeys on MSDN
eg:
if (Control.ModifierKeys == Keys.Control)
{
//...
}
I've done exactly that just recently:
static class NativeMethods
{
public static bool IsControlKeyDown()
{
return (GetKeyState(VK_CONTROL) & KEY_PRESSED) != 0;
}
private const int KEY_PRESSED = 0x8000;
private const int VK_CONTROL = 0x11;
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern short GetKeyState(int key);
}
To test the code, create a new Console Application and use the following main method:
static void Main(string[] args)
{
for (int i = 0; i < 100; i++)
{
Console.WriteLine(NativeMethods.IsControlKeyDown());
System.Threading.Thread.Sleep(100);
}
}
Can't you just catch the Ctrl
being pressed on the form that is displayed, since it seems that this form is also a part of the appication, and relay the click to the "formless" part of the code using an event that the form can invoke?
I might misunderstand what you are after here, but it seems like an option at least.
精彩评论