2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
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.
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.
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.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
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/
33 using System.Collections.Generic;
35 using System.Globalization;
38 namespace LibCECTray.controller.applications
40 class KeyInput : ApplicationAction
42 public KeyInput(ApplicationController controller) :
43 base(controller, ActionType.SendKey)
47 public KeyInput(ApplicationController controller, WindowsAPI.VirtualKeyCode keyCode) :
48 base(controller, ActionType.SendKey)
53 public static ApplicationAction FromString(ApplicationController controller, string value)
55 KeyInput retVal = new KeyInput(controller);
56 var split = value.Trim().Split(' ');
57 foreach (var item in split)
59 var param = retVal.GetParameterFromString(item);
61 if (ushort.TryParse(param, NumberStyles.AllowHexSpecifier, null, out iValue))
62 retVal.AddKey((WindowsAPI.VirtualKeyCode)iValue);
72 foreach (var input in _input)
73 if (input.Data.Keyboard.Flags == 0)
79 public override string AsString()
81 StringBuilder sb = new StringBuilder();
82 foreach (var input in _input)
84 if (input.Data.Keyboard.Flags == 0)
85 sb.AppendFormat("{0}({1:X}) ", TypePrefix, input.Data.Keyboard.KeyCode);
87 return sb.ToString().Trim();
90 public override string AsFriendlyString()
92 StringBuilder sb = new StringBuilder();
93 bool bMultipleKeys = KeyCount > 1;
94 foreach (var input in _input)
96 if (input.Data.Keyboard.Flags == 0)
98 sb.AppendFormat(bMultipleKeys ? "[{0}] " : "{0} ",
99 WindowsAPI.GetVirtualKeyName((WindowsAPI.VirtualKeyCode) input.Data.Keyboard.KeyCode));
102 return sb.ToString().Trim();
110 public override bool Empty()
112 return _input.Count == 0;
115 public override bool CanAppend(ApplicationAction value)
117 return (value.ActionType == ActionType.SendKey);
120 public override ApplicationAction Append(ApplicationAction value)
122 if (value.ActionType == ActionType.SendKey)
123 AddKey(value as KeyInput);
128 public WindowsAPI.Input[] ToArray()
130 return _input.ToArray();
133 public void AddKey(WindowsAPI.VirtualKeyCode keyCode)
139 private void AddKeyDown(WindowsAPI.VirtualKeyCode keyCode)
141 var key = new WindowsAPI.Input {
142 Type = WindowsAPI.InputType.Keyboard,
144 Keyboard = new WindowsAPI.KeyboardInput {
145 KeyCode = (UInt16) keyCode,
149 ExtraInfo = IntPtr.Zero
157 private void AddKeyUp(WindowsAPI.VirtualKeyCode keyCode)
159 var key = new WindowsAPI.Input {
160 Type = WindowsAPI.InputType.Keyboard,
162 Keyboard = new WindowsAPI.KeyboardInput {
163 KeyCode = (UInt16) keyCode,
165 Flags = (uint)WindowsAPI.KeyEvent.KeyUp,
167 ExtraInfo = IntPtr.Zero
175 public KeyInput AddKey(KeyInput input)
179 bool foundModifier = false;
182 for (int i = _input.Count - 1; i >= 0; i--)
185 if (_input[i].Data.Keyboard.Flags == 0 && (
186 (ushort)WindowsAPI.VirtualKeyCode.VK_SHIFT == _input[i].Data.Keyboard.KeyCode ||
187 (ushort)WindowsAPI.VirtualKeyCode.VK_LSHIFT == _input[i].Data.Keyboard.KeyCode ||
188 (ushort)WindowsAPI.VirtualKeyCode.VK_RSHIFT == _input[i].Data.Keyboard.KeyCode ||
190 (ushort)WindowsAPI.VirtualKeyCode.VK_CONTROL == _input[i].Data.Keyboard.KeyCode ||
191 (ushort)WindowsAPI.VirtualKeyCode.VK_LCONTROL == _input[i].Data.Keyboard.KeyCode ||
192 (ushort)WindowsAPI.VirtualKeyCode.VK_RCONTROL == _input[i].Data.Keyboard.KeyCode ||
194 (ushort)WindowsAPI.VirtualKeyCode.VK_MENU == _input[i].Data.Keyboard.KeyCode ||
195 (ushort)WindowsAPI.VirtualKeyCode.VK_LMENU == _input[i].Data.Keyboard.KeyCode ||
196 (ushort)WindowsAPI.VirtualKeyCode.VK_RMENU == _input[i].Data.Keyboard.KeyCode
199 foundModifier = true;
200 _input.Insert(i+1, input._input[0]);
201 _input.Insert(i+2, input._input[1]);
209 _input.Add(input._input[0]);
210 _input.Add(input._input[1]);
216 public override bool Transmit(IntPtr windowHandle)
218 var inputAr = ToArray();
219 if (inputAr.Length == 0)
224 return WindowsAPI.SendInputTo(windowHandle, (uint) inputAr.Length, inputAr,
225 System.Runtime.InteropServices.Marshal.SizeOf(typeof (WindowsAPI.Input)));
229 Console.Error.WriteLine(e.Message);
234 public ApplicationAction RemoveItem(int index)
237 var downKeyIndex = 0;
239 bool downFound = false;
241 for (int i = 0; i < _input.Count; i++)
243 if (_input[i].Data.Keyboard.Flags == 0 && !downFound)
245 if (index == current)
252 else if(_input[i].Data.Keyboard.Flags != 0 && downFound)
254 if (_input[i].Data.Keyboard.KeyCode == _input[downKeyIndex].Data.Keyboard.KeyCode)
262 WindowsAPI.Input downKey = _input[downKeyIndex];
263 WindowsAPI.Input upKey = _input[upKeyIndex];
265 _input.Remove(downKey);
266 _input.Remove(upKey);
271 public override ApplicationAction RemoveKey(int index)
275 foreach (var input in _input)
277 if (input.Data.Keyboard.Flags == 0)
279 var tmp = string.Format(KeyCount > 1 ? "[{0}] " : "{0} ",
280 WindowsAPI.GetVirtualKeyName((WindowsAPI.VirtualKeyCode) input.Data.Keyboard.KeyCode));
281 current += tmp.Length;
282 if (index <= current)
283 return RemoveItem(item);
291 private readonly List<WindowsAPI.Input> _input = new List<WindowsAPI.Input>();