Solved

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

Posted on 2009-05-17
4
429 Views
Last Modified: 2013-12-17
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 ?
0
Comment
Question by:dvplayltd
  • 2
  • 2
4 Comments
 
LVL 8

Expert Comment

by:dericstone
ID: 24405373
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

0
 

Author Comment

by:dvplayltd
ID: 24405459
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.

0
 
LVL 8

Accepted Solution

by:
dericstone earned 500 total points
ID: 24405579
In that case, you might try using PInvoke on GetAsyncKeyState as in the attached snippet.
using System.Runtime.InteropServices;
 

namespace WindowsFormsApplication1

{

  public partial class Form1 : Form

  {

    const int VK_SHIFT = 0x0010;
 

    [DllImport("User32.dll")]

    public static extern short GetAsyncKeyState(int vKey);
 

    public Form1()

    {

      InitializeComponent();
 

      short key = GetAsyncKeyState(VK_SHIFT);

      checkBox1.Checked = (key & 0x8000) != 0;

    }

  }

}

Open in new window

0
 

Author Closing Comment

by:dvplayltd
ID: 31582323
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.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with deleting records by comparing two files using VB.NET 8 56
Please explain "Multi-Tenant Services" 5 65
Entity Framework 7 32
Javascript and Jquery not firing 9 42
Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now