Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

API to Capture Keystrokes

Posted on 1998-09-08
2
464 Views
Last Modified: 2008-02-01
I need an app to run in the background capturing keystrokes into a sequential file.  VB's OnKeypress won't work for my purposes...I've searched for an API call do do this but without success.
0
Comment
Question by:RMiller
2 Comments
 
LVL 12

Expert Comment

by:fulscher
ID: 1433589
You'll need some callbacks and your own window handler to capture the messages.
0
 
LVL 14

Accepted Solution

by:
waty earned 100 total points
ID: 1433590
you have to use the API :

' *** Get the state of a key at any time
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

Try the following code :
Create a new project, and add a Picture Box and a Command Button to the form. Then paste the following code into the form:


Private m_bPlay As Boolean
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub cmdPlay_Click()
Dim i As Long
Dim iLast As Long

    If cmdPlay.Caption = "&Stop" Then
        m_bPlay = False
        cmdPlay.Caption = "&Play"
    Else
        cmdPlay.Caption = "&Stop"
        m_bPlay = True
        i = 1
        Do
            ' Determine if the left or right keys are
            ' pressed:
            If (GetAsyncKeyState(vbKeyLeft)) Then
                ' Diminish the colour
                i = i - 1
            ElseIf (GetAsyncKeyState(vbKeyRight)) Then
                ' Increase the colour
                i = i + 1
            End If
           
            ' Colour within bounds:
            If (i < 1) Then i = 15
            If (i > 15) Then i = 1
           
            ' If colour has changed, change the display:
            If (iLast <> i) Then
                With Picture1
                    .Cls
                    .ForeColor = QBColor(i)
                    ' Generate a RGB complement for the background:
                    .BackColor = &HFFFFFF And (Not QBColor(i))
                    .CurrentX = 64 * Screen.TwipsPerPixelX
                    .CurrentY = 64 * Screen.TwipsPerPixelY
                   
                    Picture1.Print Hex$(QBColor(i))
                End With
            End If
            iLast = i
           
            ' This is here to stop the animation
            ' getting too fast to see:
            Sleep 25
            ' Ensure we can still click buttons etc
            DoEvents
           
        Loop While m_bPlay
           
    End If
   
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    If (cmdPlay.Caption = "&Stop") Then
        cmdPlay_Click
    End If
End Sub



When you click on the command button, the code starts a DoEvents loop in which it checks if the Right Key or Left Key are pressed. When one is pressed, it changes the background and foreground colours of the Picture Box and displays the QBColor code.


0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SSRS expression Issue finding a string 10 90
Child Form in front 4 49
backup program with robocopy 6 45
how to loop through and process two columns in excel 8 22
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question