I need to be able to detect that the shift key is being held, but I don't want to use events or global variables to determine that. Is there an API in C# that lets you ask what keys are currently pressed instead of using the event?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
You can install a low-level keyboard hook.
Form.ModifierKeys
(static property)Not sure if this available in C# but you can call GetAsyncKeyState. This method returns the state of a key at the time the method is called.
To call it from C# you'll need to use interop like any other Win32 API.
This will also be
true
if another modifier key is also down (eg, Ctrl+Shift). If you want to check whether Shift alone is pressed without any other modifiers, useNote that even this will be
true
if another non-modifier is down (Eg, Shift+A). If you want to check whether Shift and only Shift is pressed, you'll have to use an API call.If you're in a class that inherits
Control
(such as a form), you can remove theControl
qualifier. (static
properties don't need qualifiers in inherited classes)