keyboard state in .net

does someone know the place to get the keyboard state

Thanks,
Micki-WeaverAsked:
Who is Participating?
 
Bob LearnedConnect With a Mentor Commented:
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
 
Bob LearnedCommented:
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
 
Micki-WeaverAuthor Commented:
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Micki-WeaverAuthor Commented:
anyone have a non api .net solution
0
 
Micki-WeaverAuthor Commented:
I'll give point for that I just think a non api call would be cleaner
0
 
Bob LearnedCommented:
Good luck trying to find a solution.  I would be very interested to hear one.

Bob
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
Micki-WeaverAuthor Commented:
I think I can:)

I can't make a My object in vb???
do I need to add something??
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
If you have VB.Net 2005 then it should be there...

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


0
 
Micki-WeaverAuthor Commented:
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
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
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
 
Micki-WeaverAuthor Commented:
I need to get a bit more then the VB stuff gives but thank you so much for your thoughts, advice and code
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
Micki-WeaverAuthor Commented:
The non API way
System.Windows.Forms.SendKeys.Send("CAPSLOCK")


you can thank - hatem72

0
 
Micki-WeaverAuthor Commented:
ok it works for all but control keys
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.