Key Tracking Using API not working
-
I made a lil' application that tracks wht key has been pressed... I have called api function... but it's not tracking keys like A or a , R... It's working for Enter, backspace pretty well..... Why it is not working for alphabets.....
Declare Function GetAsyncKeyState Lib "user32" Alias "GetAsyncKeyState" (ByVal vKey As Keys) As Integer Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick If GetAsyncKeyState(Keys.A) > 0 Then MsgBox("A") Else End If End Sub End Class
-
I made a lil' application that tracks wht key has been pressed... I have called api function... but it's not tracking keys like A or a , R... It's working for Enter, backspace pretty well..... Why it is not working for alphabets.....
Declare Function GetAsyncKeyState Lib "user32" Alias "GetAsyncKeyState" (ByVal vKey As Keys) As Integer Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick If GetAsyncKeyState(Keys.A) > 0 Then MsgBox("A") Else End If End Sub End Class
I don't think that is the way to track the keys being pressed (with a timer?) Should you not be hooking into the keyboard message loop? Look at the Docs for it; GetAsyncKeyState Function Determines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to GetAsyncKeyState. Your Timer Tick is more than likely missing the keyboard event.
Dave Find Me On: Web|Facebook|Twitter|LinkedIn CPRepWatcher now available as Packaged Chrome Extension, visit my articles for link.
-
I don't think that is the way to track the keys being pressed (with a timer?) Should you not be hooking into the keyboard message loop? Look at the Docs for it; GetAsyncKeyState Function Determines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to GetAsyncKeyState. Your Timer Tick is more than likely missing the keyboard event.
Dave Find Me On: Web|Facebook|Twitter|LinkedIn CPRepWatcher now available as Packaged Chrome Extension, visit my articles for link.
I have put msg box jes to chk if it's working or not.... Ma question is that y this code not working for keys A B C... It's only working for few keys like enter, Ctrl, Escape.....
-
I have put msg box jes to chk if it's working or not.... Ma question is that y this code not working for keys A B C... It's only working for few keys like enter, Ctrl, Escape.....
It doesn't work because the key goes through make/break before your Timer fires. Your timer has to fire between the key make and key break message, which will happen VERY quickly. Seriously, this is a very bad way of see which keys were hit. A better method would be a keyboard hook.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
It doesn't work because the key goes through make/break before your Timer fires. Your timer has to fire between the key make and key break message, which will happen VERY quickly. Seriously, this is a very bad way of see which keys were hit. A better method would be a keyboard hook.
A guide to posting questions on CodeProject[^]
Dave KreskowiakYeah i kno Timer concept is not rite.....but ma focus was on keys 'A','B'....y it's not working for them.. rather works totally fine for enter.... Here some changes i made
Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress If GetAsyncKeyState(Keys.Enter) > 0 Then MsgBox("Enter") Else End If End Sub
Even this is not working although it's done on Key press event....