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

  • Thread starter Thread starter ShanonLK
  • Start date Start date
S

ShanonLK

Guest
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. 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";
}
}
}

Continue reading...
 
Back
Top