Link to home
Start Free TrialLog in
Avatar of dvplayltd
dvplayltdFlag for Bulgaria

asked on

How to get keyboard press SHIFT outside keyboard event holder function?

Hi experts!

  Im using C# 2008 , WinForm NET 2. I want in one procedure to get state of keyboard (to check for example will user  is press SHIFT button, or other button)

 I need this to make better speed of development, I create database application and want when I press SHIFT to skip check for user/ name / password . (Of course I have addition technique which can do this only  in my DEV computer, not in production).

 So in general I need to check for press SHIFT state, but on function which are not some of keyboard event holder. How to do this ?
Avatar of dericstone
dericstone
Flag of Germany image

Add KeyDown and KeyUp events to your top-level Form as in the attached snippet. Then store the state of the shift key. Make sure you have enabled the KeyPreview property of the Form.

namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
 
      this.KeyPreview = true;
    }
 
    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
      checkBox1.Checked = e.Shift;
    }
 
    private void Form1_KeyUp(object sender, KeyEventArgs e)
    {
      checkBox1.Checked = e.Shift;
    }
  }
}

Open in new window

Avatar of dvplayltd

ASKER

To dericstone

You send me rigth code, but it is not for my case.
I do not get at all
private void Form1_KeyDown(object sender, KeyEventArgs e)
or any event connected with key because I already press it BEFORE start my form

I need to produce such behavour - press SHIFT, then start my program and go without check.

I need someway to scan current state of keyboard, but outside of event which hande keyboard event. That is question.

ASKER CERTIFIED SOLUTION
Avatar of dericstone
dericstone
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yeee. This work, thank you ! P.S: I COM world I use API and think that in NET have some diffrent object ... but look that is same.