问题描述
我想捕获在应用程序启动期间是否按下了修饰键(以确定是全屏显示还是窗口显示).
I wanted to capture whether a modifier key was being pressed during application startup (to determine fullscreen or windowed).
在主窗口构造函数中,我尝试检查 Keyboard.Modifiers 枚举,以查看Shift是否关闭.它总是显示无".
In the main window constructor I tried checking the Keyboard.Modifiers enum to see if Shift is down. It always showed 'None'.
因此,我尝试通过启动 DispatcherTimer 并检查其Tick事件是否移位.多数民众赞成在似乎很好.
So I tried a different approach by starting off a DispatcherTimer and checking for shift in its Tick event. Thats seems to work fine.
问题是,这是最好的(唯一的)方法吗?为什么修饰符在窗口构造函数中未返回正确的值?
Question is, is this the best (only) way to do this? And why does the modifier not return the correct value in the window constructor?
推荐答案
Keyboard.Modifiers
是正确的类/属性.
我建议在处理程序中检查FrameworkElement.Loaded
事件的修饰符.
I would suggest checking the modifiers in a handler for the FrameworkElement.Loaded
event.
在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();
}
}
这篇关于应用程序启动期间的键盘修饰符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!