Commit | Line | Data |
---|---|---|
f017f3c4 LOK |
1 | /* |
2 | * This file is part of the libCEC(R) library. | |
3 | * | |
4 | * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved. | |
5 | * libCEC(R) is an original work, containing original code. | |
6 | * | |
7 | * libCEC(R) is a trademark of Pulse-Eight Limited. | |
8 | * | |
9 | * This program is dual-licensed; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
22 | * | |
23 | * | |
24 | * Alternatively, you can license this library under a commercial license, | |
25 | * please contact Pulse-Eight Licensing for more information. | |
26 | * | |
27 | * For more information contact: | |
28 | * Pulse-Eight Licensing <license@pulse-eight.com> | |
29 | * http://www.pulse-eight.com/ | |
30 | * http://www.pulse-eight.net/ | |
31 | */ | |
32 | ||
33 | using System.Collections.Generic; | |
34 | using System; | |
35 | using System.Globalization; | |
36 | using System.Text; | |
37 | ||
38 | namespace LibCECTray.controller.applications | |
39 | { | |
40 | class KeyInput : ApplicationAction | |
41 | { | |
42 | public KeyInput(ApplicationController controller) : | |
43 | base(controller, ActionType.SendKey) | |
44 | { | |
45 | } | |
46 | ||
47 | public KeyInput(ApplicationController controller, WindowsAPI.VirtualKeyCode keyCode) : | |
48 | base(controller, ActionType.SendKey) | |
49 | { | |
50 | AddKey(keyCode); | |
51 | } | |
52 | ||
53 | public static ApplicationAction FromString(ApplicationController controller, string value) | |
54 | { | |
55 | KeyInput retVal = new KeyInput(controller); | |
56 | var split = value.Trim().Split(' '); | |
57 | foreach (var item in split) | |
58 | { | |
59 | var param = retVal.GetParameterFromString(item); | |
60 | ushort iValue; | |
61 | if (ushort.TryParse(param, NumberStyles.AllowHexSpecifier, null, out iValue)) | |
62 | retVal.AddKey((WindowsAPI.VirtualKeyCode)iValue); | |
63 | } | |
64 | return retVal; | |
65 | } | |
66 | ||
67 | public int KeyCount | |
68 | { | |
69 | get | |
70 | { | |
71 | int count = 0; | |
72 | foreach (var input in _input) | |
73 | if (input.Data.Keyboard.Flags == 0) | |
74 | count++; | |
75 | return count; | |
76 | } | |
77 | } | |
78 | ||
79 | public override string AsString() | |
80 | { | |
81 | StringBuilder sb = new StringBuilder(); | |
82 | foreach (var input in _input) | |
83 | { | |
84 | if (input.Data.Keyboard.Flags == 0) | |
85 | sb.AppendFormat("{0}({1:X}) ", TypePrefix, input.Data.Keyboard.KeyCode); | |
86 | } | |
87 | return sb.ToString().Trim(); | |
88 | } | |
89 | ||
90 | public override string AsFriendlyString() | |
91 | { | |
92 | StringBuilder sb = new StringBuilder(); | |
93 | bool bMultipleKeys = KeyCount > 1; | |
94 | foreach (var input in _input) | |
95 | { | |
96 | if (input.Data.Keyboard.Flags == 0) | |
97 | { | |
98 | sb.AppendFormat(bMultipleKeys ? "[{0}] " : "{0} ", | |
99 | WindowsAPI.GetVirtualKeyName((WindowsAPI.VirtualKeyCode) input.Data.Keyboard.KeyCode)); | |
100 | } | |
101 | } | |
102 | return sb.ToString().Trim(); | |
103 | } | |
104 | ||
105 | public int Size() | |
106 | { | |
107 | return _input.Count; | |
108 | } | |
109 | ||
110 | public override bool Empty() | |
111 | { | |
112 | return _input.Count == 0; | |
113 | } | |
114 | ||
115 | public override bool CanAppend(ApplicationAction value) | |
116 | { | |
117 | return (value.ActionType == ActionType.SendKey); | |
118 | } | |
119 | ||
120 | public override ApplicationAction Append(ApplicationAction value) | |
121 | { | |
122 | if (value.ActionType == ActionType.SendKey) | |
123 | AddKey(value as KeyInput); | |
124 | ||
125 | return this; | |
126 | } | |
127 | ||
128 | public WindowsAPI.Input[] ToArray() | |
129 | { | |
130 | return _input.ToArray(); | |
131 | } | |
132 | ||
133 | public void AddKey(WindowsAPI.VirtualKeyCode keyCode) | |
134 | { | |
135 | AddKeyDown(keyCode); | |
136 | AddKeyUp(keyCode); | |
137 | } | |
138 | ||
139 | private void AddKeyDown(WindowsAPI.VirtualKeyCode keyCode) | |
140 | { | |
141 | var key = new WindowsAPI.Input { | |
142 | Type = WindowsAPI.InputType.Keyboard, | |
143 | Data = { | |
144 | Keyboard = new WindowsAPI.KeyboardInput { | |
145 | KeyCode = (UInt16) keyCode, | |
146 | Scan = 0, | |
147 | Flags = 0, | |
148 | Time = 0, | |
149 | ExtraInfo = IntPtr.Zero | |
150 | } | |
151 | } | |
152 | }; | |
153 | ||
154 | _input.Add(key); | |
155 | } | |
156 | ||
157 | private void AddKeyUp(WindowsAPI.VirtualKeyCode keyCode) | |
158 | { | |
159 | var key = new WindowsAPI.Input { | |
160 | Type = WindowsAPI.InputType.Keyboard, | |
161 | Data = { | |
162 | Keyboard = new WindowsAPI.KeyboardInput { | |
163 | KeyCode = (UInt16) keyCode, | |
164 | Scan = 0, | |
165 | Flags = (uint)WindowsAPI.KeyEvent.KeyUp, | |
166 | Time = 0, | |
167 | ExtraInfo = IntPtr.Zero | |
168 | } | |
169 | } | |
170 | }; | |
171 | ||
172 | _input.Add(key); | |
173 | } | |
174 | ||
175 | public KeyInput AddKey(KeyInput input) | |
176 | { | |
177 | if (input != null) | |
178 | { | |
179 | foreach (var item in input._input) | |
180 | _input.Add(item); | |
181 | } | |
182 | return this; | |
183 | } | |
184 | ||
185 | public override bool Transmit(IntPtr windowHandle) | |
186 | { | |
187 | var inputAr = ToArray(); | |
188 | if (inputAr.Length == 0) | |
189 | return false; | |
190 | ||
191 | try | |
192 | { | |
193 | return WindowsAPI.SendInputTo(windowHandle, (uint) inputAr.Length, inputAr, | |
194 | System.Runtime.InteropServices.Marshal.SizeOf(typeof (WindowsAPI.Input))); | |
195 | } | |
196 | catch (Exception e) | |
197 | { | |
198 | Console.Error.WriteLine(e.Message); | |
199 | return false; | |
200 | } | |
201 | } | |
202 | ||
203 | public ApplicationAction RemoveItem(int index) | |
204 | { | |
205 | //both keyup and keydown | |
206 | if (index * 2 + 1 < _input.Count) | |
207 | { | |
208 | _input.RemoveAt(index * 2); | |
209 | _input.RemoveAt(index * 2); | |
210 | } | |
211 | return this; | |
212 | } | |
213 | ||
214 | public override ApplicationAction RemoveKey(int index) | |
215 | { | |
216 | var current = 0; | |
217 | var item = 0; | |
218 | foreach (var input in _input) | |
219 | { | |
220 | if (input.Data.Keyboard.Flags == 0) | |
221 | { | |
222 | var tmp = string.Format(KeyCount > 1 ? "[{0}] " : "{0} ", | |
223 | WindowsAPI.GetVirtualKeyName((WindowsAPI.VirtualKeyCode) input.Data.Keyboard.KeyCode)); | |
224 | current += tmp.Length + 1; | |
225 | if (index <= current) | |
226 | return RemoveItem(item); | |
227 | item++; | |
228 | } | |
229 | } | |
230 | ||
231 | return this; | |
232 | } | |
233 | ||
234 | private readonly List<WindowsAPI.Input> _input = new List<WindowsAPI.Input>(); | |
235 | } | |
236 | } |