开发者

Hot-keys for buttons

How do I make hotkeys for button开发者_如何学运维s, which will activate it, like I pressed the button?

Keep in mind I'm new at programming.


Assuming you want a normal alt-hotkey, in WPF you simply name the button in Xaml with the hotkey you want preceeded by an underscore, for example:

<Button x:Name="MyButton">_Hotkey</Button>

Pressing Alt+H will activate (meaning, click) the button.


You can use the GetKeyState function in Windows. Windows functions must be referenced by APIs, since they are not by default included in the .NET Framework as such.

Assuming you only need to detect one keystroke, GetKeyState or GetAsyncKeyState would probably be best for that. GetKeyState will wait until a specific key is pressed and then get the state of that key, and GetAsyncKeyState will get the state of a key at the time the function is called.

The declaration of GetKeyState is as follows:

<DllImport("user32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True)> _
Public Shared Function GetKeyState(ByVal virtualKeyCode As Integer) As Short
End Function

Put that inside the class where you wish to use the function.

The virtualKeyCode argument of the function is the keycode of the key you are trying to get the state of. You can see a list of all the virtual keycodes here: http://msdn.microsoft.com/en-us/library/ms927178.aspx

The GetAsyncKeyState declaration is almost the same as the GetKeyState declaration.

<DllImport("user32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True)> _
Public Shared Function GetAsyncKeyState(ByVal virtualKeyCode As Integer) As Short
End Function

Using both the GetKeyState and GetAsyncKeyState is pretty simple From Microsoft's own documentation (http://msdn.microsoft.com/en-us/library/ms646293(v=vs.85).aspx) it says:

If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. However, you should not rely on this last behavior; for more information, see the Remarks.

Given the above information, here's an example of GetAsyncKeyState usage:

Dim X As Integer = GetAsyncKeyState(KeyCodeHere)
If X = 1 Or X = -32767 Then
    'your keycode was pressed or released.
End If

If you need to get notified when the user presses any kind of key, a keyboard hook would be more appropriate.

Lastly, you may also use the RegisterHotkey API to get notified by Windows when specific hotkeys are pressed.


Alt+Key is one way, like AresAvatar posted above.

if you want some more possibilities you can use the KeyBinding class


Are you developping Desktop or Web? for desktop, try MY.COMPUTER.KEYBOARD. ..... encapsulates much (all?) of GetKeyState mentioned above.

like AltKeyDow, ShiftKeyDown CtrlKeyDoen ScrollLock NumLock

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜