In the near future with the students to play Death vs Fire Shadow, to miss childhood, suddenly feel that playing with the keyboard is not carefree, so the initiation of a virtual handle to write the idea.
My idea is to implement the handle on the mobile device (IOS, Android), listen on the computer, use the socket to establish a persistent connection, send the operation code to the listening software on the computer through the mobile device, and handle the event through the opcode.
On the server of the socket, it is very convenient to use the socket on the mobile device, it is easy to connect the mobile device and the computer, to establish the channel, and to make the socket client on the server. The focus of this paper is to implement key events on the computer, including the processing of key combinations.
We assume that the virtual handle has 4+6 keys, respectively, up and down, 1-6 function keys, the operation code is sent to 0~9, when all the keys released, the operation code to send is-1.
In order to implement the key operation, it is necessary to use USER32.DLL's keybd_event (byte bvk, byte bscan, int dwFlags, int dwextrainfo) function, the first parameter is the key code, the second and fourth fill 0, The third represents whether the button is pressed or released, 0 means press, 2 means release.
Because C # cannot call those macros directly, the key code input number to implement, key code corresponding table is as follows:
<span style= "FONT-SIZE:18PX;" > virtual key corresponding value corresponding key Vk_lbutton 1 left mouse button Vk_rbutton 2 right mouse button Vk_cancel 3 Cancelvk_mbutton 4 middle mouse button Vk_xbutton1 5 vk_xbutton2 6 Vk_bac K 8 Backspacevk_tab 9 tabvk_clear clearvk_return entervk_shift Shiftvk_control + ctrlvk_menu-AltVK_PAUSE P Ausevk_capital Caps Lockvk_kana-Vk_hangul Vk_junja-vk_final Vk_hanja Vk_kanji VERT Vk_nonconvert vk_accept vk_modechange vk_space, Spacevk_prior page Upvk_next Endvk_home Homevk_left PNs left arrowvk_up-up Arrowvk_right-Arrowvk_down 4 0 down Arrowvk_select selectvk_print printvk_execute executevk_snapshot Snapshotvk_insert 4 6 Deletevk_help Help 0 1 2 3, 4, 5, 6, 7, 8, 9, A, B,, D, E, F, G, H, 74 J-K-L-M-N-O-P-ba-Q-R-S-N-A-T-U-V-W-X-Y-Vk_rwin-Vk_apps 93 Vk_sleep VK_NUMPAD0 96 Keypad 0vk_numpad1 97 Numpad 1vk_numpad2 98 keypad 2vk_numpad3 99 KEYPAD 3VK_NUMPAD4 100 Keypad 4VK_NUMPAD5 10 1 numpad 5vk_numpad6 102 Numpad 6VK_NUMPAD7 103 numpad 7vk_numpad8 104 Numpad 8vk_numpad9 105 keypad 9vk_multiply 106 keypad *vk_add 107 keypad +vk_separator 108 Keypad entervk_subtract 109 Numpad-vk_decimal 110 keypad. Vk_divide 111 keypad/vk_f1 F1VK_F2 113 f2vk_f3, F3vk_f4, F4vk_f5, F5vk_f6 117 F6vk_f7 118 F7vk_f8 119 F8VK_F9 1 20 F9VK_F10 121 F10VK_F11 122 F11VK_F12 123 F12VK_F13 124 VK_F14 125 VK_F15 126 VK_F16 127 VK_F17 128 VK_F18 129 VK_F19 13 0 VK_F20 131 VK_F21 132 VK_F22 133 VK_F23 134 VK_F24 135 VK_NUMLOCK 144 Num LockVK_SCROLL 145 ScrollVK_LSHIFT 160 VK_RSHIF T 161 Vk_lcontrol 162 Vk_rcontrol 163 Vk_lmenu 164 Vk_rmenu 165 Vk_browser_back 166 Vk_browser_forward 167 VK_BROWSER_REFR 168 Vk_browser_stop 169 Vk_browser_search ESH vk_browser_favorites 171 vk_browser_home 172 VK_VOLUME_MUTE 173 VolumeMu Tevk_volume_down 174 volumedownvk_volume_up 175 Volumeupvk_media_next_track 1177 vk_media_stop 178 Vk_media_play_pause 179 vk_launch_mail vk_media_prev_track vk_launch_media_select 181 VK_LAU NCH_APP1 182 vk_launch_app2 183 Vk_oem_1 186; : Vk_oem_plus 187 = +vk_oem_comma 188 Vk_oem_minus 189-_vk_oem_period 191/? Vk_oem_3 192 ' ~vk_oem_4 219 [{Vk_oem_5 220 \ | VK_OEM_6 221 ] }VK_OEM_7 222 ' "VK_OEM_8 223 VK_OEM_102 226 VK_PACKET 231 VK_PROCESSKEY 229 VK_ATTN 246 VK_CRSEL 247 VK_EX SEL 248 vk_ereof 249 Vk_play + vk_zoom 251 vk_noname 252 vk_pa1 253 vk_oem_clear 254</span>
In order to implement the key combination, for each key to be processed, you should call the function to implement the key press, and note that the key that has been pressed cannot be pressed repeatedly, when all the keys are loose to start, to clear all the keys down, in order to achieve this goal, using dynamic array ArrayList to record the keys that have been pressed.
To use ArrayList, refer to:
<span style= "FONT-SIZE:18PX;" >using system.collections;</span>
ArrayList The Main method is to contains determine whether the element is inside the array, clear removes all elements, add elements.
We determine whether ArrayList contains the element when each key is pressed, does not include the call function to hold the key down, and the key code is added to the array, otherwise it does not move.
When the keys are all released, you should traverse the ArrayList array, let all pressed keys be released, and then empty the ArrayList.
With this logic, we can implement any keystroke event.
Here is a detailed explanation of how each module is implemented:
"Press and release of Keys"
Because you want to introduce a DLL, add a reference:
<span style= "FONT-SIZE:18PX;" >using system.runtime.interopservices;</span>
then reference a DLL to handle the keyboard:
<span style= "FONT-SIZE:18PX;" >[dllimport ("USER32. DLL ")]public static extern void keybd_event (byte bvk, byte bscan, int dwFlags, int dwextrainfo);</span>
This function is described above the key processing function, the direct call can simulate keyboard operation, in order to achieve the above business logic, the key press when the first to determine whether it has been pressed, and then processing, encapsulating a function processing button:
Where Al is a dynamic array, it is defined as follows:
Static ArrayList Al = new ArrayList (0);
<span style= "FONT-SIZE:18PX;" >static void Presskey (byte keycode) { if (!al. Contains (keycode)) { al. ADD (keycode); keybd_event (keycode, 0, 0, 0);} } </span>
This enables the press of the key and avoids repeated presses.
When the keys are all released, release all pressed keys and empty the AL:
foreach (Byte key in AL) { keybd_event (key, 0, 2, 0);} Al. Clear ();
"Socket Implementation"
First quote:
Using system.net;using System.Net.Sockets;
Then use a function to implement the socket's listener
static void Runsocket () {//Set server IP address IPAddress ip = ipaddress.parse ("<ip address >"); Socket clientsocket = new socket (addressfamily.internetwork, SocketType.Stream, protocoltype.tcp); try {clientsocket.connect (new IPEndPoint (IP, < port number >));//configure server IP with Port Console . WriteLine ("Connection server succeeded"); } catch {Console.WriteLine ("The connection server failed, please press ENTER to exit!") "); Return }//Receive data via Clientsocket while (true) {int receivelength = Clientsocket.rec Eive (result); String str = Encoding.ASCII.GetString (result, 0, receivelength); Console.WriteLine ("<" + str + ">"); int option =-1; try {option = Int. Parse (str); } catch {}//Console.writEline ("option =" + option); state = option; } }
The reason for defining a function is to listen to the Socket,socket received in the child thread to parse the operation code, after the successful resolution is assigned to State,state is the main thread to process the operation code, which represents the press button.
Open the code for the socket thread, which is written in the main function:
Thread t = new thread (runsocket); T.start ();
The next part is to deal with the different keys, the following paste the full source code, which is a C # console program:
For security, I have my own IP and port have been removed, if you want to use this source code, you need to pay attention to the following things:
The ①socket server can send characters 0~9,-1 according to the action of the handle.
② computer-side programs remain open, debug and run state.
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Using system.net;using system.net.sockets;using system.threading;using system.runtime.interopservices;using System.collections;namespace socketclient{class Program {static int state = 0; Static ArrayList Al = new ArrayList (0); private static byte[] result = new byte[1024]; [DllImport ("USER32. DLL ")] public static extern void keybd_event (byte bvk, byte bscan, int dwFlags, int dwextrainfo); static void Presskey (byte keycode) {if (!al. Contains (KeyCode)) {al. ADD (KeyCode); keybd_event (keycode, 0, 0, 0); }} static void Main (string[] args) {thread t = new Thread (runsocket); T.start (); Al. Clear (); state =-1; while (true) {if (state! =-1) {SWItch (state) {case 0://W = Presskey (87); Break Case 1://S = Presskey (83); Break Case 2://A = Presskey (65); Break Case 3://D = Presskey (68); Break Case 4://J = Presskey (74); Break Case 5://K = Presskey (75); Break Case 6://L = Presskey (76); Break Case 7://U = Presskey (85); Break Case 8://I =Presskey (73); Break Case 9://O = Presskey (79); Break }} else {foreach (byte key in AL) { keybd_event (Key, 0, 2, 0); } al. Clear (); }}}} static void Runsocket () {//Set server IP address IPAddress IP = IPAddress . Parse ("42.96.168.162"); Socket clientsocket = new socket (addressfamily.internetwork, SocketType.Stream, protocoltype.tcp); try {clientsocket.connect (new IPEndPoint (IP, 12345));//configure server IP with Port console.write Line ("Connection server succeeded"); } catch {Console.WriteLine ("The connection server failed, please press ENTER to exit!") "); Return } Receive data via Clientsocket while (true) {int receivelength = clientsocket.receive (Result ); String str = Encoding.ASCII.GetString (result, 0, receivelength); Console.WriteLine ("<" + str + ">"); int option =-1; try {option = Int. Parse (str); } catch {}//Console.WriteLine ("option =" + option); state = option; } } }}
Check the countless data to achieve such a few functions, it is not easy, I hope that the need for you to help.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Use C#+socket to implement virtual handles that are controlled with a mobile device