Detecting keyboard input?
I am trying to make a program that will count how many times I press each key, so like a normal key logger but instead of being able to read what I wrote I just want to know the number of times a key was pressed.
As my program will be running in the background how do I detect if a key has been pressed, and which key it was?
If you know how to detect mouse clicks as well that would be good...
I am using VB in VS 2010
Re: Detecting keyboard input?
Use the KeyDown event for each button, the mouse has a mouseDown similair event for whichever button you want to count.
Put your code in the event to count it and identify it.
Re: Detecting keyboard input?
Yes but how do I detect which button has been pressed? All I can find on the internet is how to detect non-alphanumeric keys such as shift using e.shift?
Re: Detecting keyboard input?
Take a look at the e argument in that event. There are several different properties of that event. The one you probably want is the KeyCode. I would suggest a Dictionary(of Integer,Integer) where the key is e.KeyCode, and the value is the count. If the Dictionary doesn't have the KeyCode, then you would add it (setting the value to 1 at the same time). If the Dictionary does have the KeyCode, then you would increment the value for that key.
Re: Detecting keyboard input?
vb.net Code:
Private Sub Form1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Label1.Text = e.KeyCode.ToString 'or .KeyData or .KeyValue
End Sub
Re: Detecting keyboard input?
is that going to work if his form doesn't have focus? he said it would be running in the background. An api solution might be more appropriate.
the final post in this thread may hold the solution you need.
http://www.vbforums.com/showthread.p...tAsyncKeyState
although he's using the old way of declaring api calls which is why he needs to specify x86 in compiler settings.
it should have been this:
<DllImport("user32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True)> Public Shared Function GetAsyncKeyState(ByVal vkey As Integer) As Short
End Function
Re: Detecting keyboard input?
Hi,
I'm still learning VB and haven't used an API yet. Would you mind explaining/showing exactly how to use it?
Thanks
Re: Detecting keyboard input?
basically once you've declared it, you call it like any other function. It will even show up in your intellisense showing you what is expected in each entry.
i cut and pasted this from another thread:
Code:
Public Class Form1
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Integer) As Integer
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If GetAsyncKeyState(Keys.L) Then
MsgBox("Tada!")
End If
End Sub
End Class
google is your friend.
Re: Detecting keyboard input?
i forgot to mention that getasynckeystate will oddly enough also detect mouse clicks because values 1 and 2 are the left and right mouse buttons. Since all you are doing is counting you could simply call every key in a for next loop (1 to 128) in place of the keys.L in the sample. Every one you get a true value back on is going to increment your number. However there is also a way to detect if the key is pinned down or held down. There's a flag in the return value that not only says if it is pressed or up but if it's been pressed since the last time you checked. I suggest you study up on the api call so you don't end up counting the same keypress multiple times.
Re: Detecting keyboard input?
Thanks, you have no idea how helpful you've been!