windows 如何模拟远程桌面会话的键盘输入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/345147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:42:55  来源:igfitidea点击:

How to simulate keybard input to a remote desktop session?

windowsautomationtimeoutremote-desktop

提问by Ian Boyd

i'm trying to send fake keyboard input to an application that's running in a Remote Desktop session. i'm using:

我正在尝试将假键盘输入发送到在远程桌面会话中运行的应用程序。我正在使用:

Byte key = Ord("A");

keybd_event(key, 0, 0, 0); // key goes down
keybd_event(key, 0, KEYEVENTF_KEYUP, 0); // key goes up

Now this code does send the character "a" to any local window, but it will not send to the remote desktop window.

现在此代码确实将字符“a”发送到任何本地窗口,但不会发送到远程桌面窗口。

What that means is i use Remote Desktop to connect to a server, and i then open Notepad on that server. If i manually punch keys on the keyboard: they appear in Notepad's editor window. But keybd_event's fake keyboard input not causing "a"'s to appear in Notepad.

这意味着我使用远程桌面连接到服务器,然后在该服务器上打开记事本。如果我在键盘上手动打键:它们会出现在记事本的编辑器窗口中。但是keybd_event 的假键盘输入不会导致“a”出现在记事本中。

How can i programtically send fake keyboard input to an application running inside a remote desktop connection, from an application running on the local machine?

如何从本地计算机上运行的应用程序以编程方式将假键盘输入发送到远程桌面连接内运行的应用程序?



Nitpickers Corner

吹毛求疵的角落

In this particular case i want to do this becase i'm trying to defeat an idle-timeout. But i could just as well be trying to

在这种特殊情况下,我想这样做是因为我试图击败空闲超时。但我也可以尝试

  • perform UI automation tests
  • UI stress tests
  • UI fault finding tests
  • UI unit tests
  • UI data input tests
  • UI paint tests
  • or UI resiliance tests.
  • 执行 UI 自动化测试
  • 界面压力测试
  • UI故障查找测试
  • 界面单元测试
  • UI数据输入测试
  • UI绘画测试
  • 或 UI 弹性测试。

In other words, my reasons for wanting it aren't important

换句话说,我想要它的原因并不重要

Note:The timeout may be from remote desktop inactivity, or perhaps not. i don't know, and it doesn't affect my question.

注意:超时可能来自远程桌面不活动,也可能不是。我不知道,这不影响我的问题。

采纳答案by Ian Boyd

Answer

回答

Although Microsft says you don't need to, and you should not, send the OEM code, you need to send the OEM scan codes. In this example i need to send the OEM scan codes for

尽管 Microsft 说您不需要也不应该发送 OEM 代码,但您需要发送 OEM 扫描代码。在这个例子中,我需要发送 OEM 扫描代码

  • key A goes down
  • key A goes up
  • 键 A 下降
  • 键 A 上升

There is a picture of a chart on CodeProjectthat lists the make and break scan codes for various keys:

CodeProject上有一张图表图片,列出了各种键的通断扫描码:

alt text

替代文字

In my case the original calls to keybd_event need to be changed to:

在我的情况下,对 keybd_event 的原始调用需要更改为:

Byte key = Ord("A");

keybd_event(key, 0x1E, 0, 0); // key goes down
keybd_event(key, 0x9E, KEYEVENTF_KEYUP, 0); // key goes up

i tested this, and it works. So all is well.

我测试了这个,它有效。所以一切都很好。

回答by VonC

May be you can execute an autoitscript with PsExec, a light-weight telnet-replacement that lets you execute processes on other systems, complete with full interactivity for console applications, without having to manually install client software.

可能是你可以执行的AutoIt与脚本PSEXEC,重量轻的telnet更换,让您在执行其他系统,并配备完整的交互控制台应用程序的过程,而无需手动安装客户端软件。

(AutoIt is quite capable to send any signal (keys or other) to any window application, and could be launched with PsExec on the remote desktop)

(AutoIt 能够将任何信号(键或其他)发送到任何窗口应用程序,并且可以在远程桌面上使用 PsExec 启动)

An AutoIt script like KillSaver, for instance, is designed to move the mouse to avoid any prolong idle time on a computer!

例如,像KillSaver这样的 AutoIt 脚本旨在移动鼠标以避免在计算机上延长空闲时间!

回答by selwyn

This worked very well thank you. In order to get the keyboard scan code one can use:

这很有效,谢谢。为了获得键盘扫描码,可以使用:

int scan;
scan = MapVirtualKey(key & 0xff, 0);
keybd_event(key, scan, 0, 0); // key goes down
keybd_event(key, scan | 0x80, KEYEVENTF_KEYUP, 0); // key goes up

回答by Guus Geurkink

You could use SendMessage(); It's really a much better simulator for keys. Well, good luck on this!

你可以使用 SendMessage(); 这真的是一个更好的钥匙模拟器。好吧,祝你好运!