Results 1 to 8 of 8

Thread: advanced - need help - replacing keystrokes

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Pittsburgh, PA
    Posts
    329

    Exclamation

    my program runs always on top but doesnt have focus. when the user types a keystroke the program needs to intercept what was typed and replace the letter with something else.

    Example: user hits the "a" key.
    program sees "a" key has been hit.
    program adds one to the ASCII value of "a" (which makes it "b")
    program displays the letter "b"

    the process is invisible to the user. when the hit the "a" key, a "b" appears (just as if the user had hit "b" in the beginning.

    also please remember my program does this even if it doesnt have the focus.


    thanks for your help!
    ______________

  2. #2
    Guest
    Try this:

    Code:
    Private Declare Function GetAsyncKeyState _
    Lib "user32" (ByVal vKey As Long) As Integer
    
    Private Sub Timer1_Timer()
    If GetAsyncKeyState(vbKeyA) Then Text1.text = "b"
    End Sub

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Pittsburgh, PA
    Posts
    329

    close...

    i tried the code and it is working but the is a problem with using a timer. if i set the interval something slow and you type at a moderate speed, the program only picks up one of keys you press every interval. if you set the interval very fast it sometimes detects 1 key press as to or 3 (because you hold the key in for a split second every time you hit it.)


    is there some way to detect the key press without the timer?

    thanks for your suggestion. i'd appreciate any more you have.
    ______________

  4. #4
    Guest
    Set the Timer's interval at 10. Hopefully, that will help.

    But if it doesn't, you can use this code:

    Code:
    Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyA Then
    Text1.SelStart = 0
    Text1.SelLength = Len(Text1.Text)
    Text1.Text = "b"
    SendKeys "{BACKSPACE}"
    Else
    End If
    End Sub

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Pittsburgh, PA
    Posts
    329

    getting closer...

    well, we are getting close. the last part of the problem is the keypress even only works when you press a key and text1 has focus.
    how can i use the key press even to detect a keypress even if my form doesnt have focus

    (i need to detect the keypress when a user is typing in another application)


    thanks again. any more help you can give would be appreciated.
    ______________

  6. #6
    Guest
    You'd have to go back to using a Timer (Interval = 10?) and the GetAsyncKeyState api function.

    Code:
    Private Sub Timer1_Timer()
    If KeyCode = vbKeyA Then
    SendKeys "{BACKSPACE}" 'erase the A
    SendKeys "b" 'put in the B
    Else
    End If
    End Sub

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Pittsburgh, PA
    Posts
    329
    i'm going to use this like you suggested:

    Private Declare Function GetAsyncKeyState _
    Lib "user32" (ByVal vKey As Long) As Integer

    Private Sub Timer1_Timer()
    If GetAsyncKeyState(vbKeyA) Then Text1.text = "b"
    End Sub



    how do i loop through all the different keys to see which one was pressed (like vbkeyA - vbkeyZ and also the numbers and the $#@ stuff)
    ?
    ______________

  8. #8
    Guest
    Code:
    Private Sub Timer1_Timer()    
    Dim iKey As Integer    
    For iKey = 3 To 255       
    If GetAsyncKeyState(iKey) Then Debug.Print Chr(iKey) 
    Next 
    End Sub

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