Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Key Tracking Using API not working

Key Tracking Using API not working

Scheduled Pinned Locked Moved Visual Basic
json
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    greendragons
    wrote on last edited by
    #1

    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

    D 1 Reply Last reply
    0
    • G greendragons

      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

      D Offline
      D Offline
      DaveAuld
      wrote on last edited by
      #2

      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.

      G 1 Reply Last reply
      0
      • D DaveAuld

        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.

        G Offline
        G Offline
        greendragons
        wrote on last edited by
        #3

        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.....

        D 1 Reply Last reply
        0
        • G greendragons

          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.....

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          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

          G 1 Reply Last reply
          0
          • D 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 Kreskowiak

            G Offline
            G Offline
            greendragons
            wrote on last edited by
            #5

            Yeah 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....

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups