Results 1 to 6 of 6

Thread: Button Presses Inside / Outside the form

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2006
    Posts
    51

    Button Presses Inside / Outside the form

    My last post has somehow been corrupt, so i will repost it...

    I am looking for a way to know if a user has hit "Ctrl + A" anywhere on the screen.

    I tried this:
    VB Code:
    1. Private Sub Form_KeyPress(KeyAscii As Integer)
    2. If KeyAscii = vbKeyControl And KeyAscii = vbKeyA Then
    3. '-------------
    4. 'Code
    5. MsgBox "Congrats, you just hit Ctrl + A :D"
    6. '-------------
    7. End If
    8. End Sub
    (it dosn't work)

    This can either be hit inside the form, our ourside the form.

    Thanks
    ~Cody

  2. #2

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2006
    Posts
    51

    Re: Button Presses Inside / Outside the form

    VB Code:
    1. Private Const MOD_ALT = &H1
    2. Private Const MOD_CONTROL = &H2
    3. Private Const MOD_SHIFT = &H4
    4. Private Const PM_REMOVE = &H1
    5. Private Const WM_HOTKEY = &H312
    6. Private Type POINTAPI
    7.     x As Long
    8.     y As Long
    9. End Type
    10. Private Type Msg
    11.     hWnd As Long
    12.     Message As Long
    13.     wParam As Long
    14.     lParam As Long
    15.     time As Long
    16.     pt As POINTAPI
    17. End Type
    18. Private Declare Function RegisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
    19. Private Declare Function UnregisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long) As Long
    20. Private Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As Msg, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
    21. Private Declare Function WaitMessage Lib "user32" () As Long
    22. Private bCancel As Boolean
    23.  
    24. Private Sub ProcessMessages()
    25.     Dim Message As Msg
    26.     Do While Not bCancel
    27.         WaitMessage
    28.         If PeekMessage(Message, Me.hWnd, MOD_CONTROL, vbKeyD, PM_REMOVE) Then
    29.             '--------
    30.             'Code for Ctrl+D
    31.             MsgBox "Ctrl+D"
    32.             '--------
    33.         End If
    34.         DoEvents
    35.     Loop
    36. End Sub
    37. Private Sub Form_Load()
    38.      Dim ret As Long
    39.      bCancel = False
    40.      autoClick = RegisterHotKey(Me.hWnd, 1, MOD_CONTROL, vbKeyA)
    41.      autoClickHold = RegisterHotKey(Me.hWnd, 2, MOD_CONTROL, vbKeyS)
    42.      autoRelease = RegisterHotKey(Me.hWnd, 3, MOD_CONTROL, vbKeyD)
    43.      autoRightClick = RegisterHotKey(Me.hWnd, 4, MOD_CONTROL, vbKeyF)
    44.      autoDoubleClick = RegisterHotKey(Me.hWnd, 5, MOD_CONTROL, vbKeyG)
    45.      Show
    46.     ProcessMessages
    47.  
    48. End Sub
    49.  
    50. Private Sub Form_Unload(Cancel As Integer)
    51.     bCancel = True
    52.     Call UnregisterHotKey(Me.hWnd, 1)
    53.     Call UnregisterHotKey(Me.hWnd, 2)
    54.     Call UnregisterHotKey(Me.hWnd, 3)
    55.     Call UnregisterHotKey(Me.hWnd, 4)
    56.     Call UnregisterHotKey(Me.hWnd, 5)
    57. End Sub

    From that example, i got this.

    the msgbox "Ctrl + D" pops up at the begining of the form. And if i atempt to press it. Nothing happens.

    Thanks
    ~Cody

    ( Sorry about that double post, it was coming up as a blank page on my browser, and i thought it might be corrupt ^^; )

  4. #4
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Button Presses Inside / Outside the form

    VB Code:
    1. Private Sub ProcessMessages()
    2.     Dim Message As Msg
    3.     'loop until bCancel is set to True
    4.     Do While Not bCancel
    5.         'wait for a message
    6.         WaitMessage
    7.         'check if it's a HOTKEY-message
    8.         If PeekMessage(Message, Me.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
    9.             Select Case Message.wParam
    10.                 Case 1
    11.                     Debug.Print "Ctrl+A"
    12.                 Case 2
    13.                     Debug.Print "Ctrl+S"
    14.                 Case 3
    15.                     Debug.Print "Ctrl+D"
    16.             End Select
    17.         End If
    18.         'let the operating system process other events
    19.         DoEvents
    20.     Loop
    21. End Sub
    22.  
    23. Private Sub Form_Load()
    24.     bCancel = False
    25.    
    26.     RegisterHotKey Me.hWnd, 1, MOD_CONTROL, vbKeyA
    27.     RegisterHotKey Me.hWnd, 2, MOD_CONTROL, vbKeyS
    28.     RegisterHotKey Me.hWnd, 3, MOD_CONTROL, vbKeyD
    29.    
    30.     Me.Show
    31.     ProcessMessages
    32. End Sub
    33.  
    34. Private Sub Form_Unload(Cancel As Integer)
    35.     bCancel = True
    36.    
    37.     Call UnregisterHotKey(Me.hWnd, 1)
    38.     Call UnregisterHotKey(Me.hWnd, 2)
    39.     Call UnregisterHotKey(Me.hWnd, 3)
    40. End Sub

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2006
    Posts
    51

    Re: Button Presses Inside / Outside the form

    Thanks alot anyway to call on a command button like?

    i hit CTRL+A and it dose the exact same thing as a command button would do... so what this will look like is...

    VB Code:
    1. Select Case Message.wParam
    2.                 Case 1
    3.                     opt1.value = 1
    4.                 Case 2
    5.                     opt2.value = 1
    6.                 Case 3
    7.                     opt3.value = 1
    8.             End Select

    In the "Command1" code it will check to see which one is slected... but would something like this work?

    VB Code:
    1. Case1
    2. opt1.value = 1
    3. Command1_Click()

    Or somethign similar,

    Thanks again
    ~Cody Woolaver

  6. #6
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Button Presses Inside / Outside the form

    yes you can call the sub directly, or you could do Command1.Value = True

    however, it would be better programming practice to create a sub that does what needs to be done and call that from both the _Click event and from the select case, e.g.:
    VB Code:
    1. Private Sub Command1_Click()
    2.     MySub
    3. End Sub
    4.  
    5. Private Sub ProcessMessages()
    6.     '
    7.     Case1
    8.         opt1.value = True
    9.         MySub
    10.     '
    11. End Sub
    12.  
    13. Private Sub MySub()
    14.     ' Do your thing
    15. 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