user32 - how to press and hold a key C# -
what trying accomplish in console program able press , hold key, using user32.dll. know not sending extended key. dont think sending scancode right either. , think passing right flag hold key.. know have key up. of right need key pushed down. appreciated, of right code below not work
using system; using system.collections.generic; using system.linq; using system.runtime.interopservices; using system.text; using system.threading; using system.threading.tasks; namespace pressand_hold { class program { [dllimport("user32.dll")] public static extern void keybd_event(byte bvk, byte bscan, uint dwflags, uint dwextrainfo); const int vk_up = 0x26; //up key const int vk_down = 0x28; //down key const int vk_left = 0x25; const int vk_right = 0x27; const uint keyeventf_keyup = 0x0002; const uint scancode = 0x0008; const int key_0 = 11; internal enum scancodeshort : short { key_9 = 10, key_a = 30, key_b = 48, key_c = 46, key_d = 32, key_e = 18, key_f = 33, key_g = 34, key_h = 35, key_i = 23, key_j = 36, key_k = 37, key_l = 38, key_m = 50, key_n = 49, key_o = 24, key_p = 25, key_q = 16, key_r = 19, key_s = 31, key_t = 20, key_u = 22, key_v = 47, key_w = 17, key_x = 45, key_y = 21, key_z = 44, } static void main(string[] args) { thread.sleep(2000); // push v key keybd_event((byte)scancodeshort.key_v, 0x45, 0, 0); // release v key keybd_event((byte)scancodeshort.key_v, 0x45, keyeventf_keyup, 0); console.writeline("done"); console.read(); } } }
2nd , 3rd arguments of keybd_event
wrong.
take pinvoke definition , msdn
2nd argument should 0x45
3rd argument cannot 8. must 0 push key.
may :
static void main(string[] args) { thread.sleep(2000); // push v key keybd_event((byte)scancodeshort.key_v, 0x45, 0, 0); console.writeline("done"); console.read(); }
a scancode nothing else visual representation of string (numbers or alphanumerics).
be careful code. 47 0x2f en hexa, , vk_help in virtual key codes
key_v = 86,
* complete code *
using system; using system.runtime.interopservices; using system.threading; namespace pressand_hold { internal class program { internal enum scancodeshort : short { key_0 = 48, key_1, key_2, key_3, key_4, key_5, key_6, key_7, key_8, key_9, key_a = 65, key_b, key_c, key_d, key_e, key_f, key_g, key_h, key_i, key_j, key_k, key_l, key_m, key_n, key_o, key_p, key_q, key_r, key_s, key_t, key_u, key_v, key_w, key_x, key_y, key_z, } [dllimport("user32.dll")] public static extern void keybd_event(byte bvk, byte bscan, uint dwflags, uint dwextrainfo); private static void main(string[] args) { thread.sleep(2000); keybd_event((byte)scancodeshort.key_v, 0x45, 0, 0); console.writeline("done"); console.read(); } } }
to hold key, use loop (while()
, for()
, etc...)
Comments
Post a Comment