将 WPF 窗口聚焦在快捷键上

发布于 2024-11-16 04:32:50 字数 116 浏览 7 评论 0原文

我创建了一个 WPF 应用程序,如果用户按 ctl + alt + s,我的 WPF 应用程序文本框需要获得焦点。

示例:如果您按 ctl+w,单词 web 将自动获得焦点。

提前致谢。

I have created a WPF application, where if user press ctl + alt + s, my WPF application textbox needs to be focused.

Example: if you press ctl+w, automatically word web will get focused.

Thanks in advance.

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

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

发布评论

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

评论(3

我早已燃尽 2024-11-23 04:32:50

使用 InputBindings,定义一个 KeyBinding 并创建一个 进行聚焦的命令

  <Window.InputBindings>
    <KeyBinding  Command="{Binding MyFocusCommand}" Key="S" Modifiers="Control+Alt"/>
  </Window.InputBindings>

Use InputBindings, define a KeyBinding and create a command which does the focusing.

  <Window.InputBindings>
    <KeyBinding  Command="{Binding MyFocusCommand}" Key="S" Modifiers="Control+Alt"/>
  </Window.InputBindings>
东京女 2024-11-23 04:32:50

您可以订阅 PreviewKeyDown 事件:

private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (Keyboard.Modifiers == (ModifierKeys.Control | ModifierKeys.Alt) && 
        e.Key == Key.S)
    { 
        textBox1.Focus();
    }
}

You can subscribe to PreviewKeyDown event:

private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (Keyboard.Modifiers == (ModifierKeys.Control | ModifierKeys.Alt) && 
        e.Key == Key.S)
    { 
        textBox1.Focus();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文