|
-
Oct 1st, 2012, 02:59 PM
#1
Thread Starter
Lively Member
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
-
Oct 1st, 2012, 03:06 PM
#2
Addicted Member
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.
.
We must question the story logic of having an all-knowing all-powerful God, who creates faulty Humans, and then blames them for his own mistakes.
GENE RODDENBERRY
.
http://www.tachufind.com
.
-
Oct 1st, 2012, 04:29 PM
#3
Thread Starter
Lively Member
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?
-
Oct 1st, 2012, 04:44 PM
#4
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.
My usual boring signature: Nothing
 
-
Oct 1st, 2012, 04:46 PM
#5
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
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Oct 1st, 2012, 07:43 PM
#6
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
Last edited by Lord Orwell; Oct 1st, 2012 at 07:51 PM.
-
Oct 5th, 2012, 05:08 AM
#7
Thread Starter
Lively Member
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
-
Oct 11th, 2012, 10:01 AM
#8
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.
-
Oct 11th, 2012, 10:02 AM
#9
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.
Last edited by Lord Orwell; Oct 11th, 2012 at 10:09 AM.
-
Oct 11th, 2012, 01:55 PM
#10
Thread Starter
Lively Member
Re: Detecting keyboard input?
Thanks, you have no idea how helpful you've been!
Tags for this Thread
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
|