我想捕获在应用程序启动期间是否按下修改键(以确定全屏或窗口)。

在主窗口构造函数中,我尝试检查 Keyboard.Modifiers 枚举以查看Shift是否已关闭。它总是显示“无”。

所以我尝试了一种不同的方法,开始 DispatcherTimer 并检查其Tick事件的转变。这似乎工作正常。

问题是,这是最好(唯一)的方法吗?为什么修饰符没有在窗口构造函数中返回正确的值?

有帮助吗?

解决方案

Keyboard.Modifiers 是要使用的正确类/属性。

我建议检查 FrameworkElement.Loaded 事件的处理程序中的修饰符。

InitializeComponent()之后的 Window 构造函数中:

this.Loaded += new RoutedEventHandler(Window_Loaded);

void Window_Loaded(object sender, RoutedEventArgs e)
{
    // Examine Keyboard.Modifiers and set fullscreen/windowed
    if ((Keyboard.Modifiers & ModifierKeys.Shift) > 0)
    {
        //SetFullscreen();
    }
}

其他提示

我打赌 Keyboard.Modifiers 正在使用 GetKeyState ,这可能在你的消息循环发送了它的第一条消息之前不起作用。 GetAsyncKeyState 对你有用(通过P / Invoke我猜,除非有一种.net方式调用它我不知道)。

很棒的摘要 ...第二个链接有很棒的代码可以在此过程中显示...只需添加Josh G的代码(从这个问题的答案到第二个链接中的项目):

在InitializeComponent()之后的Window构造函数中:

this.Loaded += new RoutedEventHandler(Window_Loaded);

void Window_Loaded(object sender, RoutedEventArgs e)
{
    // Examine Keyboard.Modifiers and set fullscreen/windowed
    if ((Keyboard.Modifiers & ModifierKeys.Shift) > 0)
    {
        MessageBox.Show("The Window is Shifty...");
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top