Keyboard modifiers during application startup

后端 未结 3 347
梦毁少年i
梦毁少年i 2021-01-22 17:36

I wanted to capture whether a modifier key was being pressed during application startup (to determine fullscreen or windowed).

In the main window constructor I tried che

相关标签:
3条回答
  • 2021-01-22 18:15

    Great summary... the second link has great code to show it in the process... Just add Josh G's code (from the answer in this question to the project in the second link) :

    In the Window constructor after InitializeComponent():

    this.Loaded += new RoutedEventHandler(Window_Loaded);
    

    And:

    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...");
        }
    }
    
    0 讨论(0)
  • Keyboard.Modifiers is the right class/property to use.

    I would suggest checking the modifiers in a handler for the FrameworkElement.Loaded event.

    In the Window constructor after InitializeComponent():

    this.Loaded += new RoutedEventHandler(Window_Loaded);
    

    And:

    void Window_Loaded(object sender, RoutedEventArgs e)
    {
        // Examine Keyboard.Modifiers and set fullscreen/windowed
        if ((Keyboard.Modifiers & ModifierKeys.Shift) > 0)
        {
            //SetFullscreen();
        }
    }
    
    0 讨论(0)
  • 2021-01-22 18:20

    I bet Keyboard.Modifiers is using GetKeyState under the covers, which probably doesn't work until your message loop has dispatched its first message. GetAsyncKeyState would work for you (via P/Invoke I guess, unless there's a .net way of calling it that I don't know about).

    0 讨论(0)
提交回复
热议问题