|
-
Mar 4th, 2003, 05:25 PM
#1
Thread Starter
Lively Member
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!
-
Mar 4th, 2003, 09:58 PM
#2
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"
-
Mar 5th, 2003, 10:45 AM
#3
Thread Starter
Lively Member
I know, I can find it all over in vb6, just not vb.net. Must be some where!
-
Mar 5th, 2003, 07:44 PM
#4
PowerPoster
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.
-
Mar 5th, 2003, 09:25 PM
#5
Thread Starter
Lively Member
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!
-
Mar 5th, 2003, 10:02 PM
#6
PowerPoster
< 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.
-
Mar 5th, 2003, 10:28 PM
#7
Thanks to Lethal!....now..
....if you dont want to add a seperate class and want it in your form.. then
VB Code:
Public Class Form1
Inherits System.Windows.Forms.Form
Declare Function GetKeyboardState Lib "user32" Alias "GetKeyboardState" (ByVal pbKeyState() As Byte) As Long
'etc
'etc
'etc
Private Sub Button1_click......etc
Dim state(256) As Byte
GetKeyboardState(state)
If stateX(Keys.NumLock) = 1 Then
MsgBox("true")
Else
Msgbox("false")
End If
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"
-
Mar 5th, 2003, 10:38 PM
#8
PowerPoster
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.
-
Mar 5th, 2003, 10:43 PM
#9
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"
-
Mar 5th, 2003, 10:46 PM
#10
PowerPoster
You got it.
The System.Runtime.InteropServices namespace provides a plethra of classes useful for interoping with COM objects, and native APIs from .NET.
-
Mar 5th, 2003, 10:56 PM
#11
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"
-
Mar 6th, 2003, 10:38 AM
#12
PowerPoster
I got my raincoat on, bring it on!
-
Mar 6th, 2003, 03:04 PM
#13
Thread Starter
Lively Member
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 )
-
Mar 6th, 2003, 03:25 PM
#14
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"
-
Mar 6th, 2003, 04:14 PM
#15
Thread Starter
Lively Member
Ah, your right again 
Don't worry, my dumb questions will end soon --im getting a book soon
-
Mar 6th, 2003, 05:04 PM
#16
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"
-
Mar 6th, 2003, 06:34 PM
#17
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|