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 foreach (var item in input._input)
185 public override bool Transmit(IntPtr windowHandle)
187 var inputAr = ToArray();
188 if (inputAr.Length == 0)
193 return WindowsAPI.SendInputTo(windowHandle, (uint) inputAr.Length, inputAr,
194 System.Runtime.InteropServices.Marshal.SizeOf(typeof (WindowsAPI.Input)));
198 Console.Error.WriteLine(e.Message);
203 public ApplicationAction RemoveItem(int index)
205 //both keyup and keydown
206 if (index * 2 + 1 < _input.Count)
208 _input.RemoveAt(index * 2);
209 _input.RemoveAt(index * 2);
214 public override ApplicationAction RemoveKey(int index)
218 foreach (var input in _input)
220 if (input.Data.Keyboard.Flags == 0)
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);
234 private readonly List<WindowsAPI.Input> _input = new List<WindowsAPI.Input>();