我最近为客户开发了一个虚拟键盘应用程序。该程序几乎适用于所有程序,但某些命令喜欢{ENTER}
或{DEL}
不适用于 Citrix。是否有解决方法或替代方法SendKeys
?
编辑 1:我尝试了 SendInput 方法(Windows 输入模拟器使用 SendInput)并且 DEL 键以及箭头键仍然不起作用。但是,ENTER 键有效。
编辑2:解决了。使用两种不同版本的 Citrix 进行了测试。这个问题对我帮助很大。:
Citrix 瘦客户端使用 keybd_event 的扫描码参数,即使 MS 说它未使用并且应该为 0。您还需要为 citrix 客户端提供物理扫描码才能获取它。Citrix 客户端在使用 SendInput API 生成的键盘输入方面也存在重大问题。
我修补了Windows 输入模拟器中的代码:
// Function used to get the scan code
[DllImport("user32.dll")]
static extern uint MapVirtualKey(uint uCode, uint uMapType);
/// <summary>
/// Calls the Win32 SendInput method ...
/// </summary>
/// <param name="keyCode">The VirtualKeyCode to press</param>
public static void SimulateKeyPress(VirtualKeyCode keyCode)
{
var down = new INPUT();
down.Type = (UInt32)InputType.KEYBOARD;
down.Data.Keyboard = new KEYBDINPUT();
down.Data.Keyboard.Vk = (UInt16)keyCode;
// Scan Code here, was 0
down.Data.Keyboard.Scan = (ushort) MapVirtualKey((UInt16)keyCode, 0);
down.Data.Keyboard.Flags = 0;
down.Data.Keyboard.Time = 0;
down.Data.Keyboard.ExtraInfo = IntPtr.Zero;
var up = new INPUT();
up.Type = (UInt32)InputType.KEYBOARD;
up.Data.Keyboard = new KEYBDINPUT();
up.Data.Keyboard.Vk = (UInt16)keyCode;
// Scan Code here, was 0
up.Data.Keyboard.Scan = (ushort)MapVirtualKey((UInt16)keyCode, 0);
up.Data.Keyboard.Flags = (UInt32)KeyboardFlag.KEYUP;
up.Data.Keyboard.Time = 0;
up.Data.Keyboard.ExtraInfo = IntPtr.Zero;
INPUT[] inputList = new INPUT[2];
inputList[0] = down;
inputList[1] = up;
var numberOfSuccessfulSimulatedInputs = SendInput(2,
inputList, Marshal.SizeOf(typeof(INPUT)));
if (numberOfSuccessfulSimulatedInputs == 0)
throw new Exception(
string.Format("The key press simulation for {0} was not successful.",
keyCode));
}