-
Notifications
You must be signed in to change notification settings - Fork 1
/
Keyboard.cs
55 lines (43 loc) · 1.84 KB
/
Keyboard.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using Microsoft.UI.Input;
using System;
using System.Runtime.InteropServices;
using System.Text;
using Windows.System;
namespace Projzo.Interop
{
[StructLayout(LayoutKind.Sequential)]
public struct AcceleratorEntry
{
public byte IsVirtual;
public ushort Key;
public ushort Command;
[DllImport("user32.dll", CharSet = CharSet.Unicode, PreserveSig = true, SetLastError = true)]
public static extern IntPtr CreateAcceleratorTable([In, MarshalAs(UnmanagedType.LPArray)] AcceleratorEntry[] paccel, int cAccel);
}
public class Keyboard
{
static public bool IsKeyDown(Windows.System.VirtualKey key)
{
return InputKeyboardSource.GetKeyStateForCurrentThread(key).HasFlag(Windows.UI.Core.CoreVirtualKeyStates.Down);
}
static public bool IsKeyUp(Windows.System.VirtualKey key)
{
return InputKeyboardSource.GetKeyStateForCurrentThread(key).HasFlag(Windows.UI.Core.CoreVirtualKeyStates.None);
}
[DllImport("user32.dll")]
public static extern int ToUnicode(uint virtualKeyCode, uint scanCode, byte[] keyboardState,
[Out, MarshalAs(UnmanagedType.LPWStr, SizeConst = 64)]
StringBuilder receivingBuffer, int bufferSize, uint flags);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetKeyboardState(byte[] lpKeyState);
public static string ToUnicode(VirtualKey key)
{
StringBuilder sb = new StringBuilder(256);
byte[] keyboardState = new byte[256];
bool ret = GetKeyboardState(keyboardState);
ToUnicode((uint)key, 0, keyboardState, sb, 256, 0);
return sb.ToString();
}
}
}