SendKeys.Send 并关闭按键修饰符

发布于 2024-07-14 09:49:50 字数 351 浏览 13 评论 0原文

我想让 ^N 与树控件中的向下箭头一样工作。 我想 我只需将以下内容添加到 KeyDown 处理程序中:

SendKeys.Send("{Down}");

但这会被视为 Control-Down 箭头,因为 control 键 当前被按下。 msdn 页面 描述了如何打开 控制修饰符,但不知道如何关闭它。

谢谢, 基思

I want to make ^N work the same as Down arrow in a tree control. I thought
I'd just have to add the following to the KeyDown handler:

SendKeys.Send("{Down}");

but this gets treated as a Control-Down arrow since the control key
is currently pressed. The msdn page
describes how to turn on the
control modifier but not how to turn it off.

Thanks,
Keith

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

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

发布评论

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

评论(4

叹沉浮 2024-07-21 09:49:50

抱歉来晚了,但我想我找到了一个解决方案:

首先,导入 SetKeyboardState:

[DllImport("user32.dll")]
public static extern bool SetKeyboardState(byte[] lpKeyState);

然后,在调用 SendKeys.Send() 之前用归零的字节数组调用它:

SetKeyboardState(new byte[256]);
SendKeys.Send("your key sequence");

这对我有用。 希望这可以帮助!

Sorry to come late to the party but I think I found a solution:

First, import SetKeyboardState:

[DllImport("user32.dll")]
public static extern bool SetKeyboardState(byte[] lpKeyState);

Then, just call it with a zeroed byte array before calling SendKeys.Send():

SetKeyboardState(new byte[256]);
SendKeys.Send("your key sequence");

That worked for me. Hope this helps!

风为裳 2024-07-21 09:49:50

您可以 p/调用 keybd_event。 这将让您模拟处于“向上”和“向下”状态的按键。

You can p/invoke to keybd_event. That will let you simulate a key being both in "up" and "down" state.

疯狂的代价 2024-07-21 09:49:50

这是做到这一点的困难方法。 我承认这不是最佳方法。 我相信你已经知道这一点了。 您可能想要重载控件的 WindProc 方法。 使用 SendMsg Win32 Api。 这就是我过去如何让事情恢复正常的。

Here's the hard way of doing it. I admit this is not the optimal way to do it. I'm sure you already know this. You might want to overload the WindProc method of the control & use SendMsg Win32 Api. This is how I used get things to work back in the day.

陌若浮生 2024-07-21 09:49:50

您也可以尝试等到不再按下控制键时才KeySend,例如:

while (System.Windows.Input.Keyboard.IsKeyDown(Key.LeftCtrl))
{
    // lets try again after a while
    System.Threading.Thread.Sleep(250);
}
SendKeys.Send("{Down}");

You can also try to wait until the control key is no longer pressed and only then KeySend, e.g.:

while (System.Windows.Input.Keyboard.IsKeyDown(Key.LeftCtrl))
{
    // lets try again after a while
    System.Threading.Thread.Sleep(250);
}
SendKeys.Send("{Down}");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文