Hej! Hej! MS-Johan: Det var inte riktigt så jag tänkte utan jag ville ha den där textboxen som fångar upp vilka tangenter man vill ha till sin hotkey.Hotkeybox?
Har sett att många program har en "hotkeybox" alltså en sån som Windows använder när man ska associera en hotkey för att starta ett program, går det att få något liknande i VB6?
/MickeSv: Hotkeybox?
Visst kan man göra det. Du får använda dig av RegisterHotKey-API't
Koden nedan registrerar Ctrl-Alt-A som hotkey och ger dessutom din applikation fokus när hotkey-en triggas.
Mycket nöje! :)
<code>
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 GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" (ByVal lpString As String) As Integer
Private Declare Function GlobalDeleteAtom Lib "kernel32" (ByVal nAtom As Integer) As Integer
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 Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
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 Const MOD_ALT = &H1
Private Const MOD_SHIFT = &H4
Private Const MOD_CONTROL = &H2
Private Const MOD_WIN = &H8
Private Const WM_HOTKEY = &H312
Private Const WM_NCLBUTTONDOWN = &HA1 '
Private Const PM_NOREMOVE = &H0
Private Const PM_NOYIELD = &H2
Private Const PM_REMOVE = &H1
Private Const HWND_TOP = 0
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
Private Const SWP_SHOWWINDOW = &H40
Dim hAtom As Long, ExitForm As Boolean
Private Sub Form_Load()
Dim Retval As Long
hAtom = GlobalAddAtom("myHotkey")
'Registrera din hotkey
Retval = RegisterHotKey(Me.hwnd, hAtom, MOD_CONTROL + MOD_ALT, vbKeyA)
If Retval = 0 Then
MsgBox "Kunde inte registrera hotkey."
Call GlobalDeleteAtom(hAtom)
End If
End Sub
'Vänta på hotkey
Private Sub Form_Activate()
Dim wMsg As Msg, Retval As Long
wMsg.hwnd = Me.hwnd
Do
DoEvents
Call WaitMessage
Retval = PeekMessage(wMsg, Me.hwnd, 0&, 0&, PM_NOREMOVE)
With wMsg
If .Message = WM_HOTKEY Then
Call SetWindowPos(Me.hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW)
MsgBox "HOTKEY!!!"
.Message = 0
End If
End With
Loop Until wMsg.Message = WM_NCLBUTTONDOWN
End Sub
'Ta bort hotkey
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Debug.Print UnregisterHotKey(Me.hwnd, hAtom)
Call GlobalDeleteAtom(hAtom)
End Sub
</code>Sv: Hotkeybox?
/Micke