Results 1 to 9 of 9

Thread: KeyDown

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2000
    Location
    Whats a location?
    Posts
    516
    I have a form where I have to catch every keypress, so I've put that in the Form_KeyDown(etc.) event. The thing is, the event doesn't fire if, say, a commandbutton has the focus. I don't want to write code in all of the commands, listboxes, etc KeyDown event.

    How do I catch every single keypress?

    I am subclassing anyway, so that wouldn't be any extra trouble.

    Any help?

    pleeeeeeeeeeeeeeeeeeeeeeeaaaaaaaaaaaaaaaaaaaaaaaaase?
    Courgettes.

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Just set the KeyPreview property of the form to True.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2000
    Location
    Whats a location?
    Posts
    516
    I didn't know that, thanks a lot.
    Courgettes.

  4. #4
    Guest
    Use GetAsyncKeyState (will work for System-Wide tracking)
    Code:
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    
    Private Sub Timer1_Timer()
        'Check if A was pressed
        If GetAsyncKeyState(vbKeyA) Then MsgBox ("'A' was pressed")
    End Sub

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2000
    Location
    Whats a location?
    Posts
    516
    Thanks, Meg, but I'm only really looking for key presses if my form has the focus.
    Courgettes.

  6. #6
    New Member
    Join Date
    Jul 2000
    Posts
    4
    Is there any way to use GetAsyncKeyState to 'follow' what is being typed in other programs? I did this:

    For n = 8 To 255
    If GetAsyncKeyState(n) Then Text1.SelText = Chr(n)
    Next

    Which works to an extent, but still comes up with weird stuff for some codes, like 8 for delete etc. Also, is there any way to get lower-case characters as well as upper-case ones? Sorry if this is a bit demanding but it's something I've never come across before
    from Starsky
    VB6 Pro SP3
    I'm not stupid, I just don't post a lot. No, really...

  7. #7
    Guest
    Try this:

    Code:
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    
    Private Sub Timer1_Timer() 
    Dim iKey As Integer    
    For iKey = 3 To 255       
    If GetAsyncKeyState(iKey) Then Debug.Print iKey  
    Next
    End Sub
    The reason it is started at 3 is because 1 & 2 are the left and right mouse buttons, as Megatron pointed out.

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Getforegroundwindow api should tell you which window has focus, well you could find it in win32api.txt or using apiviewer (I don't have vb on this comp)
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  9. #9
    Guest
    If your Subclassing, why not just use it to tell whether the form has lost focus?

    Code:
    Option Explicit
    
    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal ndx As Long, ByVal newValue As Long) As Long
    Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
    ' This is used with the SetWindowLong API function.
    Const GWL_WNDPROC = -4
    
    Public Const WM_KILLFOCUS = &H8
    
    
    Dim saveHWnd As Long        ' The handle of the subclassed window.
    Dim oldProcAddr As Long     ' The address of the original window procedure
    
    Sub StartSubclassing(ByVal hWnd As Long)
        saveHWnd = hWnd
        oldProcAddr = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WndProc)
    End Sub
    
    Sub StopSubclassing()
        SetWindowLong saveHWnd, GWL_WNDPROC, oldProcAddr
    End Sub
    
    Function WndProc(ByVal hWnd As Long, ByVal uMsg As Long, _
        ByVal wParam As Long, ByVal lParam As Long) As Long
        ' Send the message to the original window procedure, and then
        ' return Windows the return value from the original procedure.
        WndProc = CallWindowProc(oldProcAddr, hWnd, uMsg, wParam, lParam)
        
    Select Case uMsg
        Case WM_KILLFOCUS
                'Form has lost focus
                Form1.WindowState = 1
        End Select
    End Function
    
    Private Sub Form_Load()
        StartSubclassing Me.hWnd
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        StopSubclassing
    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