|
-
Nov 27th, 2006, 12:45 PM
#1
Thread Starter
Member
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:
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyControl And KeyAscii = vbKeyA Then
'-------------
'Code
MsgBox "Congrats, you just hit Ctrl + A :D"
'-------------
End If
End Sub
(it dosn't work)
This can either be hit inside the form, our ourside the form.
Thanks
~Cody
-
Nov 27th, 2006, 12:59 PM
#2
Re: Button Presses Inside / Outside the form
Do a forum search for RegisterHotKey API (example)
-
Nov 27th, 2006, 01:38 PM
#3
Thread Starter
Member
Re: Button Presses Inside / Outside the form
VB Code:
Private Const MOD_ALT = &H1
Private Const MOD_CONTROL = &H2
Private Const MOD_SHIFT = &H4
Private Const PM_REMOVE = &H1
Private Const WM_HOTKEY = &H312
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type Msg
hWnd As Long
Message As Long
wParam As Long
lParam As Long
time As Long
pt As POINTAPI
End Type
Private Declare Function RegisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Private Declare Function UnregisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long) As Long
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
Private Declare Function WaitMessage Lib "user32" () As Long
Private bCancel As Boolean
Private Sub ProcessMessages()
Dim Message As Msg
Do While Not bCancel
WaitMessage
If PeekMessage(Message, Me.hWnd, MOD_CONTROL, vbKeyD, PM_REMOVE) Then
'--------
'Code for Ctrl+D
MsgBox "Ctrl+D"
'--------
End If
DoEvents
Loop
End Sub
Private Sub Form_Load()
Dim ret As Long
bCancel = False
autoClick = RegisterHotKey(Me.hWnd, 1, MOD_CONTROL, vbKeyA)
autoClickHold = RegisterHotKey(Me.hWnd, 2, MOD_CONTROL, vbKeyS)
autoRelease = RegisterHotKey(Me.hWnd, 3, MOD_CONTROL, vbKeyD)
autoRightClick = RegisterHotKey(Me.hWnd, 4, MOD_CONTROL, vbKeyF)
autoDoubleClick = RegisterHotKey(Me.hWnd, 5, MOD_CONTROL, vbKeyG)
Show
ProcessMessages
End Sub
Private Sub Form_Unload(Cancel As Integer)
bCancel = True
Call UnregisterHotKey(Me.hWnd, 1)
Call UnregisterHotKey(Me.hWnd, 2)
Call UnregisterHotKey(Me.hWnd, 3)
Call UnregisterHotKey(Me.hWnd, 4)
Call UnregisterHotKey(Me.hWnd, 5)
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 ^^; )
-
Nov 27th, 2006, 02:36 PM
#4
Re: Button Presses Inside / Outside the form
VB Code:
Private Sub ProcessMessages()
Dim Message As Msg
'loop until bCancel is set to True
Do While Not bCancel
'wait for a message
WaitMessage
'check if it's a HOTKEY-message
If PeekMessage(Message, Me.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
Select Case Message.wParam
Case 1
Debug.Print "Ctrl+A"
Case 2
Debug.Print "Ctrl+S"
Case 3
Debug.Print "Ctrl+D"
End Select
End If
'let the operating system process other events
DoEvents
Loop
End Sub
Private Sub Form_Load()
bCancel = False
RegisterHotKey Me.hWnd, 1, MOD_CONTROL, vbKeyA
RegisterHotKey Me.hWnd, 2, MOD_CONTROL, vbKeyS
RegisterHotKey Me.hWnd, 3, MOD_CONTROL, vbKeyD
Me.Show
ProcessMessages
End Sub
Private Sub Form_Unload(Cancel As Integer)
bCancel = True
Call UnregisterHotKey(Me.hWnd, 1)
Call UnregisterHotKey(Me.hWnd, 2)
Call UnregisterHotKey(Me.hWnd, 3)
End Sub
-
Nov 27th, 2006, 02:44 PM
#5
Thread Starter
Member
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:
Select Case Message.wParam
Case 1
opt1.value = 1
Case 2
opt2.value = 1
Case 3
opt3.value = 1
End Select
In the "Command1" code it will check to see which one is slected... but would something like this work?
VB Code:
Case1
opt1.value = 1
Command1_Click()
Or somethign similar,
Thanks again
~Cody Woolaver
-
Nov 27th, 2006, 02:47 PM
#6
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:
Private Sub Command1_Click()
MySub
End Sub
Private Sub ProcessMessages()
'
Case1
opt1.value = True
MySub
'
End Sub
Private Sub MySub()
' Do your thing
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|