@@ -29,116 +29,35 @@ public static class ActionUtil
2929
3030 #endregion
3131
32- private const uint KEYEVENTF_EXTENDEDKEY = 0x0001 ;
3332 private const uint KEYEVENTF_KEYUP = 0x0002 ;
34- private const uint INPUT_KEYBOARD = 1 ;
35-
36- [ StructLayout ( LayoutKind . Sequential ) ]
37- private struct INPUT
38- {
39- public uint type ;
40- public InputUnion U ;
41- }
42-
43- [ StructLayout ( LayoutKind . Explicit ) ]
44- private struct InputUnion
45- {
46- [ FieldOffset ( 0 ) ]
47- public KEYBDINPUT ki ;
48- }
49-
50- [ StructLayout ( LayoutKind . Sequential ) ]
51- private struct KEYBDINPUT
52- {
53- public ushort wVk ;
54- public ushort wScan ;
55- public uint dwFlags ;
56- public uint time ;
57- public IntPtr dwExtraInfo ;
58- }
5933
6034 [ DllImport ( "user32.dll" , SetLastError = true ) ]
61- private static extern uint SendInput ( uint nInputs , INPUT [ ] pInputs , int cbSize ) ;
35+ private static extern void keybd_event ( byte bVk , byte bScan , uint dwFlags , IntPtr dwExtraInfo ) ;
6236
6337 /// <summary>
6438 /// Simulates a key press and release.
6539 /// </summary>
6640 /// <param name="vk">The virtual key code to simulate.</param>
6741 public static void SimulateKey ( byte vk )
6842 {
69- SendKey ( vk , isKeyUp : false ) ;
70- SendKey ( vk , isKeyUp : true ) ;
43+ keybd_event ( vk , 0 , 0 , IntPtr . Zero ) ;
44+ keybd_event ( vk , 0 , KEYEVENTF_KEYUP , IntPtr . Zero ) ;
7145 }
7246
7347 /// <summary>
7448 /// Simulates pressing a key down.
7549 /// </summary>
7650 private static void KeyDown ( byte vk )
7751 {
78- SendKey ( vk , isKeyUp : false ) ;
52+ keybd_event ( vk , 0 , 0 , IntPtr . Zero ) ;
7953 }
8054
8155 /// <summary>
8256 /// Simulates releasing a key.
8357 /// </summary>
8458 private static void KeyUp ( byte vk )
8559 {
86- SendKey ( vk , isKeyUp : true ) ;
87- }
88-
89- private static void SendKey ( byte vk , bool isKeyUp )
90- {
91- var flags = isKeyUp ? KEYEVENTF_KEYUP : 0u ;
92- if ( IsExtendedKey ( vk ) )
93- {
94- flags |= KEYEVENTF_EXTENDEDKEY ;
95- }
96-
97- var input = new INPUT
98- {
99- type = INPUT_KEYBOARD ,
100- U = new InputUnion
101- {
102- ki = new KEYBDINPUT
103- {
104- wVk = vk ,
105- wScan = 0 ,
106- dwFlags = flags ,
107- time = 0 ,
108- dwExtraInfo = IntPtr . Zero
109- }
110- }
111- } ;
112-
113- _ = SendInput ( 1 , [ input ] , Marshal . SizeOf < INPUT > ( ) ) ;
114- }
115-
116- private static bool IsExtendedKey ( byte vk )
117- {
118- return vk switch
119- {
120- VK_MEDIA_NEXT_TRACK or VK_MEDIA_PREV_TRACK or VK_MEDIA_STOP or VK_MEDIA_PLAY_PAUSE => true ,
121- VK_VOLUME_MUTE or VK_VOLUME_DOWN or VK_VOLUME_UP => true ,
122- VK_LWIN => true ,
123- 0x5C => true , // VK_RWIN
124- 0x5D => true , // VK_APPS
125- 0x21 => true , // VK_PRIOR (Page Up)
126- 0x22 => true , // VK_NEXT (Page Down)
127- 0x23 => true , // VK_END
128- 0x24 => true , // VK_HOME
129- 0x25 => true , // VK_LEFT
130- 0x26 => true , // VK_UP
131- 0x27 => true , // VK_RIGHT
132- 0x28 => true , // VK_DOWN
133- 0x2C => true , // VK_SNAPSHOT (PrintScreen)
134- 0x2D => true , // VK_INSERT
135- 0x2E => true , // VK_DELETE
136- 0x6F => true , // VK_DIVIDE (Numpad /)
137- 0x90 => true , // VK_NUMLOCK
138- 0xA3 => true , // VK_RCONTROL
139- 0xA5 => true , // VK_RMENU
140- _ => false
141- } ;
60+ keybd_event ( vk , 0 , KEYEVENTF_KEYUP , IntPtr . Zero ) ;
14261 }
14362
14463 /// <summary>
0 commit comments