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!
Printable View
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!
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
I know, I can find it all over in vb6, just not vb.net. Must be some where!
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());
}
}
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! :D
< Translation >
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.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
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!
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! :D
Lethal one question:
Is this line the one required to use API's?
Imports System.Runtime.InteropServices
You got it.
The System.Runtime.InteropServices namespace provides a plethra of classes useful for interoping with COM objects, and native APIs from .NET.
Sweet! The floodgates are now open! lol :)
I got my raincoat on, bring it on! :D
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 :confused:
(Other then that, works great! Thank you :D)
Do it on Key up
and make sure KeyPreview is set to true for your form properties
Ah, your right again ;)
Don't worry, my dumb questions will end soon --im getting a book soon ;)
good..then I can ask you questions :)
lol I doubt it :rolleyes: ;)