You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.4 KiB
C#

9 months ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace POSV.Utils
{
public class InputSimulatorUtils
{
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 enum VirtualKeys : byte
{
VK_NUMLOCK = 0x90, //数字锁定键
VK_SCROLL = 0x91, //滚动锁定
VK_CAPITAL = 0x14, //大小写锁定
VK_A = 62
}
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);
keybd_event((byte)Key , 0x45 , KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP , 0);
}
}
//开启键盘大写 SetState(VirtualKeys.VK_CAPITAL, true);
//关闭键盘大写 SetState(VirtualKeys.VK_CAPITAL, false);
//开启键盘滚动锁定 SetState(VirtualKeys.VK_SCROLL, true);
//关闭键盘滚动锁定 SetState(VirtualKeys.VK_SCROLL, false);
//开启键盘数字锁定键 SetState(VirtualKeys.VK_NUMLOCK, true);
//关闭键盘数字锁定键 SetState(VirtualKeys.VK_NUMLOCK, false);
public static void SendKey(VirtualKeyCode key)
{
keybd_event((byte)key , 0 , KEYEVENTF_EXTENDEDKEY | 0 , 0);//按下
keybd_event((byte)key , 0 , KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP , 0);//释放
}
public const byte vbKeyControl = 0x11; // CTRL 键
public const byte vbKeyShift = 0x10; // SHIFT 键
public static void SendCtrlShift()
{
//模拟按下ctrl键
keybd_event(vbKeyControl , 0 , 0 , 0);
//模拟按下shift键
keybd_event(vbKeyShift , 0 , 0 , 0);
//松开按键ctrl
keybd_event(vbKeyControl , 0 , 2 , 0);
//松开按键shift
keybd_event(vbKeyShift , 0 , 2 , 0);
}
public static void SendClear(int length = 0)
{
if (length != 0)
{
for (int i = 0; i < length; i++)
{
//模拟按下back键
keybd_event((byte)VirtualKeyCode.BACK, 0, 0, 0);
//松开按键back键
keybd_event((byte)VirtualKeyCode.BACK, 0, 2, 0);
}
}
else
{
//模拟按下ctrl键
keybd_event(vbKeyControl, 0, 0, 0);
//模拟按下A键
keybd_event((byte)VirtualKeyCode.VK_A, 0, 0, 0);
//松开按键ctrl
keybd_event(vbKeyControl, 0, 2, 0);
//松开按键A
keybd_event((byte)VirtualKeyCode.VK_A, 0, 2, 0);
//模拟按下back键
keybd_event((byte)VirtualKeyCode.BACK, 0, 0, 0);
//松开按键back键
keybd_event((byte)VirtualKeyCode.BACK, 0, 2, 0);
}
}
}
}