日期:2014-05-17 浏览次数:20751 次
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading;
namespace SimulateKeys
{
static class Keyboard
{
public static void SimulateKeyStroke(char key, bool ctrl = false, bool alt = false, bool shift = false)
{
List<ushort> keys = new List<ushort>();
if (ctrl)
keys.Add(VK_CONTROL);
if (alt)
keys.Add(VK_MENU);
if (shift)
keys.Add(VK_SHIFT);
keys.Add(char.ToUpper(key));
INPUT input = new INPUT();
input.type = INPUT_KEYBOARD;
int inputSize = Marshal.SizeOf(input);
for (int i = 0; i < keys.Count; ++i)
{
input.mkhi.ki.wVk = keys[i];
bool isKeyDown = (GetAsyncKeyState(keys[i]) & 0x10000) != 0;
if (!isKeyDown)
SendInput(1, ref input, inputSize);
}
input.mkhi.ki.dwFlags =&nb