none
Use of Keyboard.IsKeyDown(Key.A) causes layout to change when DPI is set to 150% RRS feed

  • Question

  • I have a very simple C# windows application (using Visual Studio 2017 and .Net 4.5.2) that displays some text when a button is pressed.
    However, when the display scale (DPI) is changed to 150%, and when I press the button, the layout visibly changes, which is wrong.
    I change the scaling by RMB on Desktop --> Display settings --> Advanced scaling settings --> type "150" in the field [Enter a custom scaling size between 100% - 500% (not recommended)]. Note that you have to sign-out for the DPI settings to take effect.

    During investigations, I noticed that this change is caused by the use of the Keyboard (Keyboard.IsKeyDown(Key.A)) class.
    When this particular line is commented, there is no visible change in the layout, which is what I expect.

    Refer below images for further clarification;

    >>> image-before pressing the button;
    >>> image-after pressing the button - Layout has changed (using class Keyboard);
    >>> image-after pressing the button - Layout has not changed (without using class Keyboard);

    Any idea why this is happening or how to overcome this?


    Following is the code I used to recreate this issue;

    using System;
    using System.Windows.Forms;
    using System.Windows.Input;

    namespace LayoutIssue
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                this.textBox1.Text = "";
            }

            private void button1_Click(object sender, EventArgs e)
            {
                if (Keyboard.IsKeyDown(Key.A)) { }; // comment this line to see how it should work
                this.textBox1.Text = "Button Pressed";
            }
        }
    }
    • Moved by CoolDadTx Monday, January 13, 2020 6:03 PM Winforms related
    • Edited by ShanonLK Tuesday, January 14, 2020 3:49 AM To clarify steps followed
    Monday, January 13, 2020 1:00 PM

Answers

  • Hi ShanonLK,
    Sorry for late reply and my misunderstanding, I reproduce the situation by following your steps.
    It is exactly can't achieve your requirement. Then I have a suggestion that you can refer to.
    You can use KeyCode to gets the keyboard code for a KeyDown or KeyUp event insteading of Keyboard.IsKeyDown().
    Here is a simple code example.

    bool s;
    private void button1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        if (e.KeyCode.ToString()=="A") { s = true; };
    }
    private void button1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        if (e.KeyCode.ToString() == "A") { s = false; };
    }
    private void button1_Click(object sender, EventArgs e)
    {
        if (s)
        {
            this.textBox1.Text = "A Pressed";
        }
    }

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, January 17, 2020 8:44 AM

All replies

  • Hi ShanonLK,
    I viewed your description, you can set the state of "Advance scaling settings" to off. It lets Windows try to fix apps, so they are not blurry.
    You can follow steps below.
    1.Opening the settings> Advance scaling settings.

    2.Set the checkbox to off

    More details you can refer to this document.
    [About Per Monitor-DPI aware for WinForms Applications
    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 14, 2020 2:45 AM
  • Hi Daniel_Zhang-MSFT,
    Thank you for your reply.
    But I already had that "Let Windows try to fix..." checkbox set to OFF.
    Since you brought it up, I also tested with the check box set to ON, but with no luck.
    Btw, I modified my question by detailing the steps I follow when changing the display scaling.
    Thanks and Regards,
    ShanonLK

    Tuesday, January 14, 2020 3:55 AM
  • Hi ShanonLK,
    Sorry for late reply and my misunderstanding, I reproduce the situation by following your steps.
    It is exactly can't achieve your requirement. Then I have a suggestion that you can refer to.
    You can use KeyCode to gets the keyboard code for a KeyDown or KeyUp event insteading of Keyboard.IsKeyDown().
    Here is a simple code example.

    bool s;
    private void button1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        if (e.KeyCode.ToString()=="A") { s = true; };
    }
    private void button1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        if (e.KeyCode.ToString() == "A") { s = false; };
    }
    private void button1_Click(object sender, EventArgs e)
    {
        if (s)
        {
            this.textBox1.Text = "A Pressed";
        }
    }

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, January 17, 2020 8:44 AM
  • Hi Daniel Zhang,
    My problems didnt stop at Keyboard.IsKeyDown(). My actual requirement was to check for the below key combination --> Left-Shift + A + D
    Though the use of Control.KeyDown event solved the DPI layout issue, I could not use it in an effective way to suit my requirement. In the end, I ended up overriding the ProcessCmdKey of my Form as below to solve it;

            protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
            {
                    // The following values for keyData were observed when pressing the LShift+A+D combination;
                    // Visual Studio 2013 - Shift+A = 65601,   Shift+D = 65604
                    // Visual Studio 2017 - Shift+A = Keys.Shift|Keys.A,   Shift+D = Keys.Shift|Keys.D
                    if ((int)keyData == 65601 || keyData == (Keys.Shift | Keys.A))
                    {
                        bShiftAPressed = true;
                    }
                    else if ((int)keyData == 65604 || keyData == (Keys.Shift | Keys.D))
                    {
                        bShiftDPressed = true;
                    }
                    else
                    {
                        bShiftAPressed = false;
                        bShiftDPressed = false;
                    }
                    if (bShiftAPressed && bShiftDPressed)
                    {
                        // do your thing...
                    }
                return base.ProcessCmdKey(ref msg, keyData);
            }


    I thank you for helping me out initially, because it was your suggestion on KeyDown() that got me thinking in a different path.

    Friday, January 24, 2020 11:25 AM