Implement the simulation of keyboard and mouse with C # program

xiaoxiao2021-03-06  60

using System; using System.Runtime.InteropServices; using System.Text; namespace ConsoleApplication8 {class Class1 {[STAThread] static void Main (string [] args) {// Display current status of keys.Console.WriteLine ( "** BEFORE ** / r / ncap: {0} / r / nscr: {1} / r / nnum: {2} ", keyboard.getState (VirtualKeys.vk_capital)?" on ":" OFF ", Keyboard.getState (VirtualKeys .Vk_scroll? "On": "OFF", keyboard.getState (VirtualKeys.vk_numlock? "On": "OFF"); // Toggle All the keys: keyboard.setState (VirtualKeys.vk_capital,! KEYBOARD.GETSTATE VirtualKeys.VK_CAPITAL)); Keyboard.SetState (VirtualKeys.VK_SCROLL, Keyboard.GetState (VirtualKeys.VK_SCROLL));! Keyboard.SetState (VirtualKeys.VK_NUMLOCK, Keyboard.GetState (VirtualKeys.VK_NUMLOCK));! // Display new status of Keys.console.writeline ("/ r / n ** after ** / r / ncap: {0} / r / nscr: {1} / r / nnum: {2}, keyboard.getstate (VirtualKeys.vk_capital) "ON": "OFF", keyboard.getState (VirtualKeys.vk_scroll)? "On": "OFF", keyboard.getState (VirtualKeys.vk_numlock ":" OFF "); console.readline (); }} public enum virtualkeys: byte {VK_NUMLOCK = 0x90, VK_SCROLL = 0x91, VK_CAPITAL = 0x14} class Keyboard {const uint KEYEVENTF_EXTENDEDKEY = 0x1; const uint KEYEVENTF_KEYUP = 0x2; [DllImport ( "user32.dll")] static extern short GetKeyState (int nVirtKey); [DllImport ( "user32.dll")] static extern void keybd_event (byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo); public static bool GetState (VirtualKeys Key) {return (GetKeyState ((int) Key) == 1);} Public Static Void SetState (VirtualKeys Key, Bool State) {if (state! = getState (key)) {keybd_event ((byte) key, 0x45, keyeventf_extendedKey | 0, 0);

转载请注明原文地址:https://www.9cbs.com/read-111117.html

New Post(0)