C#完善上述虚拟键盘功能的代码
时间: 2024-06-08 15:06:57 浏览: 143
C#实现虚拟键盘
以下是一个示例代码,实现了在C#中使用Windows API模拟按键和鼠标事件:
```csharp
using System;
using System.Runtime.InteropServices;
public class VirtualKeyboard
{
private const int KEYEVENTF_EXTENDEDKEY = 0x1;
private const int KEYEVENTF_KEYUP = 0x2;
private const int VK_SHIFT = 0x10;
private const int VK_CONTROL = 0x11;
private const int VK_MENU = 0x12;
private const int VK_LWIN = 0x5B;
private const int VK_RWIN = 0x5C;
[DllImport("user32.dll")]
private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
private static extern bool SetCursorPos(int x, int y);
[DllImport("user32.dll")]
private static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
private static void PressKey(byte keyCode)
{
keybd_event(keyCode, 0, 0, 0);
}
private static void ReleaseKey(byte keyCode)
{
keybd_event(keyCode, 0, KEYEVENTF_KEYUP, 0);
}
public static void Type(string text)
{
foreach (char c in text)
{
byte keyCode = (byte)Char.ToUpper(c);
if (Char.IsLetterOrDigit(c))
{
PressKey(keyCode);
ReleaseKey(keyCode);
}
else
{
switch (c)
{
case ' ':
PressKey(0x20);
ReleaseKey(0x20);
break;
case '\n':
PressKey(0x0D);
ReleaseKey(0x0D);
break;
case '\t':
PressKey(0x09);
ReleaseKey(0x09);
break;
case '+':
PressKey(VK_SHIFT);
PressKey(0xBB);
ReleaseKey(0xBB);
ReleaseKey(VK_SHIFT);
break;
case '^':
PressKey(VK_CONTROL);
PressKey(keyCode);
ReleaseKey(keyCode);
ReleaseKey(VK_CONTROL);
break;
case '%':
PressKey(VK_MENU);
PressKey(keyCode);
ReleaseKey(keyCode);
ReleaseKey(VK_MENU);
break;
case '{':
PressKey(VK_SHIFT);
PressKey(0xDB);
ReleaseKey(0xDB);
ReleaseKey(VK_SHIFT);
break;
case '}':
PressKey(VK_SHIFT);
PressKey(0xDD);
ReleaseKey(0xDD);
ReleaseKey(VK_SHIFT);
break;
case '(':
PressKey(VK_SHIFT);
PressKey(0x39);
ReleaseKey(0x39);
ReleaseKey(VK_SHIFT);
break;
case ')':
PressKey(VK_SHIFT);
PressKey(0x30);
ReleaseKey(0x30);
ReleaseKey(VK_SHIFT);
break;
case '~':
PressKey(VK_SHIFT);
PressKey(0xC0);
ReleaseKey(0xC0);
ReleaseKey(VK_SHIFT);
break;
case '!':
PressKey(VK_SHIFT);
PressKey(0x31);
ReleaseKey(0x31);
ReleaseKey(VK_SHIFT);
break;
case '@':
PressKey(VK_SHIFT);
PressKey(0x32);
ReleaseKey(0x32);
ReleaseKey(VK_SHIFT);
break;
case '#':
PressKey(VK_SHIFT);
PressKey(0x33);
ReleaseKey(0x33);
ReleaseKey(VK_SHIFT);
break;
case '$':
PressKey(VK_SHIFT);
PressKey(0x34);
ReleaseKey(0x34);
ReleaseKey(VK_SHIFT);
break;
case '%':
PressKey(VK_SHIFT);
PressKey(0x35);
ReleaseKey(0x35);
ReleaseKey(VK_SHIFT);
break;
case '^':
PressKey(VK_SHIFT);
PressKey(0x36);
ReleaseKey(0x36);
ReleaseKey(VK_SHIFT);
break;
case '&':
PressKey(VK_SHIFT);
PressKey(0x37);
ReleaseKey(0x37);
ReleaseKey(VK_SHIFT);
break;
case '*':
PressKey(VK_SHIFT);
PressKey(0x38);
ReleaseKey(0x38);
ReleaseKey(VK_SHIFT);
break;
case '_':
PressKey(VK_SHIFT);
PressKey(0xBD);
ReleaseKey(0xBD);
ReleaseKey(VK_SHIFT);
break;
case '+':
PressKey(VK_SHIFT);
PressKey(0xBB);
ReleaseKey(0xBB);
ReleaseKey(VK_SHIFT);
break;
case '|':
PressKey(VK_SHIFT);
PressKey(0xDC);
ReleaseKey(0xDC);
ReleaseKey(VK_SHIFT);
break;
case ':':
PressKey(VK_SHIFT);
PressKey(0xBA);
ReleaseKey(0xBA);
ReleaseKey(VK_SHIFT);
break;
case '\"':
PressKey(VK_SHIFT);
PressKey(0xDE);
ReleaseKey(0xDE);
ReleaseKey(VK_SHIFT);
break;
case '<':
PressKey(VK_SHIFT);
PressKey(0xBC);
ReleaseKey(0xBC);
ReleaseKey(VK_SHIFT);
break;
case '>':
PressKey(VK_SHIFT);
PressKey(0xBE);
ReleaseKey(0xBE);
ReleaseKey(VK_SHIFT);
break;
case '?':
PressKey(VK_SHIFT);
PressKey(0xBF);
ReleaseKey(0xBF);
ReleaseKey(VK_SHIFT);
break;
default:
break;
}
}
}
}
public static void Click(int x, int y)
{
SetCursorPos(x, y);
mouse_event(0x2, x, y, 0, 0);
mouse_event(0x4, x, y, 0, 0);
}
public static void RightClick(int x, int y)
{
SetCursorPos(x, y);
mouse_event(0x8, x, y, 0, 0);
mouse_event(0x10, x, y, 0, 0);
}
public static void DoubleClick(int x, int y)
{
SetCursorPos(x, y);
mouse_event(0x2, x, y, 0, 0);
mouse_event(0x4, x, y, 0, 0);
mouse_event(0x2, x, y, 0, 0);
mouse_event(0x4, x, y, 0, 0);
}
public static void Scroll(int delta)
{
SendMessage(IntPtr.Zero, 0x20a, IntPtr.Zero, new IntPtr(delta * 120));
}
public static void PressWinKey()
{
PressKey(VK_LWIN);
ReleaseKey(VK_LWIN);
}
public static void PressCtrlAltDel()
{
PressKey(VK_CONTROL);
PressKey(VK_MENU);
PressKey(0x2E);
ReleaseKey(0x2E);
ReleaseKey(VK_CONTROL);
ReleaseKey(VK_MENU);
}
}
```
该类包含了模拟按键和鼠标事件的方法,其中Type方法可以接收一个字符串并模拟在键盘上键入该字符串,支持特殊字符和组合键。Click、RightClick和DoubleClick方法可以模拟鼠标左键、右键和双击事件,Scroll方法可以模拟鼠标滚轮事件,PressWinKey方法可以模拟按下Windows键,PressCtrlAltDel方法可以模拟Ctrl+Alt+Del组合键。
阅读全文