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

发布于 2024-07-10 15:03:53 字数 749 浏览 9 评论 0 原文

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

Byte key = Ord("A");

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

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

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

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


挑剔角

在这种特殊情况下,我想这样做,因为我正在尝试克服空闲超时。 但我也可以尝试

  • 执行 UI 自动化测试、
  • UI 压力测试、
  • UI 故障查找测试、
  • UI 单元测试、
  • UI 数据输入测试、
  • UI 绘制测试
  • 或 UI 弹性测试。

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

注意:超时可能是由于远程桌面不活动造成的,也可能不是。 我不知道,但这并不影响我的问题。

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.

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.

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

桃扇骨 2024-07-17 15:03:53

回答

尽管 Microsft 说您不需要,而且您不应该、发送OEM码,需要发送OEM扫码。 的 OEM 扫描代码

  • 在此示例中,我需要发送键 A 向下
  • 键 A 向上键

有一个 CodeProject 上的图表图片,其中列出了各种按键的通断扫描代码:

alt text

在我的例子中,对 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

我对此进行了测试,并且它有效。 所以一切都很好。

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

  • key A goes down
  • key A goes up

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

alt text

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

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.

浸婚纱 2024-07-17 15:03:53

也许您可以使用 autoit 脚本technet.microsoft.com/en-us/sysinternals/bb897553.aspx" rel="nofollow noreferrer">PsExec,一个轻量级的 telnet 替代品,可让您在其他系统上执行进程,完成具有控制台应用程序的完全交互性,无需手动安装客户端软件。

(AutoIt 非常有能力向任何窗口应用程序发送任何信号(按键或其他信号),并且可以在远程桌面上使用 PsExec 启动)

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

May be you can execute an autoit script 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 is quite capable to send any signal (keys or other) to any window application, and could be launched with PsExec on the remote desktop)

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

穿越时光隧道 2024-07-17 15:03:53

这非常有效,谢谢。 为了获取键盘扫描码,可以使用:

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

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
故事和酒 2024-07-17 15:03:53

您可以使用 SendMessage();
这确实是一个更好的按键模拟器。
好吧,祝你好运!

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文