Repository navigation
Expand file tree
/
Copy pathKeyBoardCommand.cs
More file actions
executable file
·112 lines (106 loc) · 3.45 KB
/
Copy pathKeyBoardCommand.cs
File metadata and controls
executable file
·112 lines (106 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Timers;
namespace Command
{
public class KeyBoardCommand:BaseCommand
{
[DllImport("user32.dll", EntryPoint = "keybd_event")]
public static extern void keybd_event(
byte bVk,
byte bScan, //一般情况下设成为 0
int dwFlags, //这里是整数类型 0 为按下,2为释放
int dwExtraInfo //这里是整数类型 一般情况下设成为 0
);
private Timer timer;
private int times = 0;
private int excuteTimes = 0;
private byte vk;
private delegate void DelegateKeyEvent(byte vk);
private DelegateKeyEvent keyhandel;
public KeyBoardCommand(string cmd):base(cmd)
{ }
protected override void ExcuteCommand()
{
if (string.IsNullOrEmpty(this.CommandText))
{
throw new ArgumentNullException("CommandText");
}
string[] cmd = CommandText.Split('|');
if (cmd.Length < 2)
{
throw new CommandParametersException("CommandText Less than 2 parameters");
}
else
{
if (cmd.Length >= 3)
{
if (int.TryParse(cmd[2], out times)&×!=0)
{
timer = new Timer();
timer.Elapsed += timer_Elapsed;
timer.Interval = 1000;
}
}
try
{
vk = byte.Parse(cmd[1]);
switch(cmd[0].ToLower())
{
case "keydown":
keyhandel = new DelegateKeyEvent(keyDown);
break;
case "keyup":
keyhandel = new DelegateKeyEvent(keyUp);
break;
case "keypress":
keyhandel = new DelegateKeyEvent(keypress);
break;
default:
throw new CommandParametersException("The command is not right");
}
if(timer!=null)
{
timer.Start();
}
else
{
if(keyhandel!=null)
keyhandel(vk);
}
}
catch
{
throw;
}
}
}
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
if(keyhandel!=null)
{
if (times < 0 || excuteTimes < times)
{
keyhandel(vk);
excuteTimes++;
}
}
}
private void keyDown(byte vk)
{
keybd_event(vk, 0, 0, 0);
}
private void keyUp(byte vk)
{
keybd_event(vk, 0, 2, 0);
}
private void keypress(byte vk)
{
keybd_event(vk, 0, 0, 0);
keybd_event(vk, 0, 2, 0);
}
}
}