/// <summary> /// Simulates a keystroke that the system can use to generate a WM_KEYUP or WM_KEYDOWN message. /// </summary> /// <param name="VirtualKey">A System.Byte structure that contains a virtual-key code representing the key with which to perform an action.</param> /// <param name="Silent">A System.Boolean structure specifying true if a sound should be generated when the keystroke is simulated; otherwise, false.</param> /// <param name="State">A KeyActionState enumeration value indicating the action that should be performed with the specified virtual-key code.</param> public static void SendKeyboardKey(byte VirtualKey, bool Silent, KeyActionState State) { int silent = Silent ? (int)KeyEvents.Silent : 0; // Note that both of the operations below will be performed if the caller // requested a key press operation (KeyActionState.Press). // If requested by the caller, send the virtual-key code as part of a key down operation. if ((State & KeyActionState.Down) > 0) { keybd_event(VirtualKey, 0, 0, silent); } // If requested by the caller, send the virtual-key code as part of a key up operation. if ((State & KeyActionState.Up) > 0) { keybd_event(VirtualKey, 0, (int)KeyEvents.KeyUp, silent); } }