Results 1 to 10 of 10

Thread: Detecting keyboard input?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    67

    Question 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

  2. #2
    Addicted Member
    Join Date
    Dec 2009
    Location
    Arizona
    Posts
    185

    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
    .

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    67

    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?

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    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

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Detecting keyboard input?

    vb.net Code:
    1. Private Sub Form1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    2.         Label1.Text = e.KeyCode.ToString 'or .KeyData or .KeyValue
    3.     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!

  6. #6
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    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.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    67

    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

  8. #8
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    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.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  9. #9
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    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.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    67

    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
  •  



Click Here to Expand Forum to Full Width