Results 1 to 17 of 17

Thread: Getting state of CAPS and NUM lock

  1. #1

    Thread Starter
    Lively Member Cagez's Avatar
    Join Date
    Oct 2002
    Posts
    121

    Talking Getting state of CAPS and NUM lock

    I've searched everywhere for this, I thought I found it when I saw System.Windows.Forms.KeyEventArgs, but I don't think so...

    Any help would be great Thanks!

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    geez.. I just looked for an hour...cant find anything yet either!

    Windows.forms.keys.numlock <- thats the key..but i cant seem to return a keystate
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3

    Thread Starter
    Lively Member Cagez's Avatar
    Join Date
    Oct 2002
    Posts
    121
    I know, I can find it all over in vb6, just not vb.net. Must be some where!

  4. #4
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Here's a class I wrote that will show you how to achieve this. The code is in C#, so if you need me to convert to VB.NET, let me know.

    Code:
    using System;
    using System.Runtime.InteropServices; 
    using System.Windows.Forms;
    
    [ComVisibleAttribute(false)] 
    
    public class KeyBoardState
    {
        [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true, CallingConvention=CallingConvention.Winapi)]
        public static extern bool GetKeyboardState(byte[] keys);
        private int KeyCode;
        
        public bool KeyState()	
        {
    	byte[] keys = new Byte[256];
    	GetKeyboardState(keys);
    	return (keys[this.KeyCode] == 1 ? true : false);	
        }
    
        public KeyBoardState(int keycode)
        {
    	this.KeyCode = keycode;
        }
    }
    
    class TestHarness
    {
        static void Main()
        {
    	KeyBoardState state = new KeyBoardState((int)Keys.NumLock);
    	Console.WriteLine(state.KeyState().ToString());
        }
    }
    Last edited by Lethal; Mar 5th, 2003 at 08:03 PM.

  5. #5

    Thread Starter
    Lively Member Cagez's Avatar
    Join Date
    Oct 2002
    Posts
    121
    Please convert it if you have the time! But can't you include C#.net code in VB.net code? I thought that was one of the selling points of the new .NET stuff.

    Anyway, I see you'r using the user32.dll, I was reading around and I saw you needed that DLL to get the state's, in vb6... I did't know you could include DLL's in VB.NET, well, not that I didn't know more like I didn't know how I've gotta learn how to do that!

  6. #6
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    < Translation >

    Code:
    Imports System
    Imports System.Runtime.InteropServices
    Imports System.Windows.Forms
    
    Module Module1
        Sub Main()
            Dim state As New KeyBoardState(CType(Keys.NumLock, Integer))
            Console.WriteLine(state.KeyState)
        End Sub
    End Module
    
    Public Class KeyBoardState
        Declare Function GetKeyboardState Lib "user32" Alias "GetKeyboardState" (ByVal pbKeyState() As Byte) As Long
        Private KeyCode As Integer
    
        Public Sub New(ByVal keycode As Integer)
            Me.KeyCode = keycode
        End Sub
    
        Public Function KeyState() As Boolean
            Dim state(256) As Byte
            GetKeyboardState(state)
            return (iif(state(me.KeyCode) = 1, true, false))
        End Function
    End Class
    Also, in order to interop with multiple .NET compliant languages, for instance C#, the C# module will have to be compiled into an assembly. Then, your VB.NET project will need to set a reference to the dll.
    Last edited by Lethal; Mar 5th, 2003 at 10:09 PM.

  7. #7
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    Thanks to Lethal!....now..
    ....if you dont want to add a seperate class and want it in your form.. then

    VB Code:
    1. Public Class Form1
    2.     Inherits System.Windows.Forms.Form
    3.     Declare Function GetKeyboardState Lib "user32" Alias "GetKeyboardState" (ByVal pbKeyState() As Byte) As Long
    4. 'etc
    5. 'etc
    6. 'etc
    7.  
    8. Private Sub Button1_click......etc
    9. Dim state(256) As Byte
    10. GetKeyboardState(state)
    11. If stateX(Keys.NumLock) = 1 Then
    12. MsgBox("true")
    13. Else
    14. Msgbox("false")
    15. End If
    16. End Sub

    Thanks Lethal...I saw something similar to your code above (C version) and Could'nt quite convert it right!
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  8. #8
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Yeah, I have a habbit of posting all my code in the VB.NET forums in C#, just to help others (myself as well) with the learning process when moving between languages. But, I also like converting my C# code to VB.NET to keep me on my toes!
    Last edited by Lethal; Mar 5th, 2003 at 10:44 PM.

  9. #9
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    Lethal one question:

    Is this line the one required to use API's?

    Imports System.Runtime.InteropServices
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  10. #10
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    You got it.
    The System.Runtime.InteropServices namespace provides a plethra of classes useful for interoping with COM objects, and native APIs from .NET.

  11. #11
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    Sweet! The floodgates are now open! lol
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  12. #12
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    I got my raincoat on, bring it on!

  13. #13

    Thread Starter
    Lively Member Cagez's Avatar
    Join Date
    Oct 2002
    Posts
    121
    KeyDown isn't working properly, its suppose to purform the code I have within it everytime a key is pressed... In that code I want to test for the the state, and lets say, show a message box... But its not working

    (Other then that, works great! Thank you )

  14. #14
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    Do it on Key up

    and make sure KeyPreview is set to true for your form properties
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  15. #15

    Thread Starter
    Lively Member Cagez's Avatar
    Join Date
    Oct 2002
    Posts
    121
    Ah, your right again

    Don't worry, my dumb questions will end soon --im getting a book soon

  16. #16
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    good..then I can ask you questions
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  17. #17

    Thread Starter
    Lively Member Cagez's Avatar
    Join Date
    Oct 2002
    Posts
    121
    lol I doubt it

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width