Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

keyboard state in .net

Posted on 2007-08-10
16
Medium Priority
?
277 Views
Last Modified: 2013-11-26
does someone know the place to get the keyboard state

Thanks,
0
Comment
Question by:Micki-Weaver
  • 8
  • 5
  • 3
16 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19672185
There are some considerations for what you need.  It depends on where you are trying to get the state (i.e. MouseDown event).

Bob
0
 

Author Comment

by:Micki-Weaver
ID: 19672190
I would like to check and see if the keyboard cap lock is on or the sift key is pressed, not an event just a check, like is left mouse button down?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 600 total points
ID: 19672372
This is what I used for keyboard state:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

public class KeyboardState
{
    [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
    private static extern Int32 GetAsyncKeyState(Int32 vKey);

    public static bool ControlKey
    {
        get { return ((GetAsyncKeyState((int)Keys.ControlKey) & 0x8000) != 0); }
    }

    public static bool AltKey
    {
        get { return ((GetAsyncKeyState((int)Keys.Menu) & 0x8000) != 0); }
    }

    public static bool ShiftKey
    {
        get { return ((GetAsyncKeyState((int)Keys.ShiftKey) & 0x8000) != 0); }
    }

    public static bool IsKeyPressed(Keys key)
    {
        return ((GetAsyncKeyState((int)key) & 0x8000) != 0);
    }

    [DllImport("user32.dll")]
    private static extern short GetKeyState(Keys nVirtKey);

    public enum KeyState
    {
        Error,
        Off,
        On,
        Down,
    }

    public static KeyState GetKeyboardState(Keys key)
    {
        short rtn = GetKeyState(key);

        switch (rtn)
        {
            case 0:
                return KeyState.Off;

            case 1:
                return KeyState.On;

            case -127:
                return KeyState.Down;
        }
        return KeyState.Error;
    }

}

Examples:
            KeyboardState.KeyState state = KeyboardState.GetKeyboardState(Keys.NumLock);

    if (KeyboardState.AltKey)

Bob
0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 

Author Comment

by:Micki-Weaver
ID: 19672385
anyone have a non api .net solution
0
 

Author Comment

by:Micki-Weaver
ID: 19672388
I'll give point for that I just think a non api call would be cleaner
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19672413
Good luck trying to find a solution.  I would be very interested to hear one.

Bob
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 19673539
There is limited support from VB.Net 2005.

How to: Determine If CapsLock is On in Visual Basic:
http://msdn2.microsoft.com/en-us/library/9c9d1fz9(VS.80).aspx

Keyboard.ShiftKeyDown Property():
http://msdn2.microsoft.com/en-us/library/microsoft.visualbasic.devices.keyboard.shiftkeydown(VS.80).aspx

Not sure if you can use these in C#...
0
 

Author Comment

by:Micki-Weaver
ID: 19673648
I think I can:)

I can't make a My object in vb???
do I need to add something??
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 19673926
If you have VB.Net 2005 then it should be there...

What are you using?  VB or C#? ...and which version?


0
 

Author Comment

by:Micki-Weaver
ID: 19673943
I'm in both, mainly C#, I can use the VB get Microsoft.VisualBasic.Devices.Keyboard(), I just can't find the single keys
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 400 total points
ID: 19674272
Not very intuitive...   =\

Click on Project --> Add Reference and Select "Microsoft.VisualBasic".

Then use code like:

        private void button1_Click(object sender, EventArgs e)
        {
            Microsoft.VisualBasic.Devices.Keyboard kb = new Microsoft.VisualBasic.Devices.Keyboard();
            if (kb.ShiftKeyDown)
            {
                label1.Text = "Shift Down";
            }
            else
            {
                label1.Text = "Shift Not Down";
            }
            if (kb.CapsLock)
            {
                label2.Text = "CapsLock Down";
            }
            else
            {
                label2.Text = "CapsLock Not Down";
            }
        }
0
 

Author Comment

by:Micki-Weaver
ID: 19685016
I need to get a bit more then the VB stuff gives but thank you so much for your thoughts, advice and code
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 19686660
Right...I did say there was only "limited" support in VB.Net 2005.

To get the state of ANY key then you really need to resort to APIs as Bob has demonstrated!
0
 

Author Comment

by:Micki-Weaver
ID: 19687239
The non API way
System.Windows.Forms.SendKeys.Send("CAPSLOCK")


you can thank - hatem72

0
 

Author Comment

by:Micki-Weaver
ID: 19687334
ok it works for all but control keys
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 19688337
System.Windows.Forms.SendKeys.Send("CAPSLOCK")

That doesn't really "GET" the keyboard state though...it only toggles the current state.  Not what ya asked for!....
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

810 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