Results 1 to 8 of 8

Thread: Managing Enter with KeyPress

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2000
    Location
    Italy
    Posts
    90
    Ok, this one is the last question for today! ;-)
    I would like the enter key to call a sub ("uguale"). I put
    the following code in the Form_KeyPress Sub:

    If KeyAscii = 13 Then
    uguale_Click
    KeyAscii = 0
    End If

    The problem is that if i click a button of the form and then
    i press Enter, it does no more call "uguale", but simulates
    a click on the button (that got the focus); i have this
    problem only with keyascii 13. Is there a way to make Enter
    call always "uguale" also if the focus is on a button or something else, without using a keypress event for each button? There are about 60 buttons... would be a little tedious! ;-)
    Thankyou

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I assume this follows on from your other thread . There isn't really a way to catch the event for them all, without subclassing. Although I expect Megatron will work something out .
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    Guest
    Try the GetAsyncKeyState API. Make a Form with a Timer and set it's Interval to 1.

    Code:
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Private Declare Function GetForegroundWindow Lib "user32" () As Long
    
    Private Sub Timer1_Timer()
        If GetForegroundWindow = Me.hWnd Then If GetAsyncKeyState(vbKeyReturn) = &H8000 Then Call uguale_Click 
    End Sub

    [Edited by Megatron on 08-06-2000 at 02:53 PM]

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Aha. Polling the keyboard - didn't think of that. One thought, though. Won't it slow down your program a bit? The odd keypress may be missed at the rather abysmal rate the VB timer moves at.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    Guest
    Yes, it can produce some rather unpredictable results, but for the most part, it can capture the EnterKey and that's basically what we are concerned about here.

  6. #6
    Guest
    I see your point regarding the speed if the Timer's interval was 500 or so. If you set it to 1 (or even 100), it will capture the keys as if you were typing in a TextBox. This API can make a great Spy application for shared computer's, i.e: if someone is writing a private E-mail, your program can capture the keys that were pressed and log them to a file.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jul 2000
    Location
    Italy
    Posts
    90

    Talking

    Well... i feel really like a newbie! ;-) Wow, that's
    fantastic...
    Eheh! Thanks a lot Megatron and parksie!

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jul 2000
    Location
    Italy
    Posts
    90
    Hi!
    Ok, i've tried the keyboard polling solution but there
    are some problems:
    1. Sometimes pressing Enter works well: does not press
    the button and calls "uguale"
    2. Sometimes it presses the button with focus and then
    calls "uguale"
    3. Sometimes it presses the button with focus and nothing
    else
    So i'm getting crazy with that! ;-) Do you imagine another
    solution? Is there a way to make buttons not to get focus
    state (or maybe, to pass focus state to the textfield as
    they're relased?)?
    If you have no other ideas, well! I'm gonna change each
    button's code... Eheh! Thanks a lot... do not get crazy with
    this...
    Thanx

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