skord писал(а):2 Ramzes Они же безобидные =)
Сам когдато делал, только с раскладкой глюки были... я забил на это и взял готовый =)
Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Sub Timer1_Timer()
dim cnt as long
For Cnt = 32 To 128
If GetAsyncKeyState(Cnt) <> 0 Then
debug.print Chr$(Cnt)
End If
Next
end sub
Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Sub Timer1_Timer()
dim cnt as long
For Cnt = 32 To 128
If GetAsyncKeyState(Cnt) < 0 Then
debug.print Chr$(Cnt) & " is pressed"
End If
Next
end sub
Amed писал(а):Точнее, так:
- Код: Выделить всё
Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Sub Timer1_Timer()
dim cnt as long
For Cnt = 32 To 128
If GetAsyncKeyState(Cnt) < 0 Then
debug.print Chr$(Cnt) & " is pressed"
End If
Next
end sub
Но это не хук... Хук исчите самостоятельно. На форуме были примеры (;
Amed писал(а):а) Код не мой
б) Так доработал бы, что зря флуд разводить.
[offtop]Достали уже, честное слово! Нет чтобы сразу сказать, что не устраивает, так не-ет, думают, что телепаты не в отпуске [/offtop]
Ramzes писал(а):Клавиатурным шпионо попахивает
Private Sub Timer1_Timer()
Dim st As Long
GetWindowThreadProcessId GetForegroundWindow, st
Me.Print st
End Sub
FleX_2004 писал(а):'Надеюсь разберешься что RemoveHook- это замочить хук а SetHook поставить его:) удачи
Public Enum HookFlags
HFMouseDown = 1
HFMouseUp = 2
HFMouseMove = 4
HFKeyDown = 8
HFKeyUp = 16
End Enum
Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpDest As Any, lpSource As Any, ByVal cBytes As Long)
Private Declare Function GetAsyncKeyState% Lib "user32" (ByVal vKey As Long)
Private Declare Function GetForegroundWindow& Lib "user32" ()
Private Declare Function GetWindowThreadProcessId& Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long)
Private Declare Function GetKeyboardLayout& Lib "user32" (ByVal dwLayout As Long)
Private Declare Function MapVirtualKeyEx Lib "user32" Alias "MapVirtualKeyExA" (ByVal uCode As Long, ByVal uMapType As Long, ByVal dwhkl As Long) 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 Const SWP_NOSIZE = &H1
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOREDRAW = &H8
Private Const WM_KEYDOWN = &H100
Private Const WM_KEYUP = &H101
Private Const WM_MOUSEMOVE = &H200
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202
Private Const WM_LBUTTONDBLCLK = &H203
Private Const WM_RBUTTONDOWN = &H204
Private Const WM_RBUTTONUP = &H205
Private Const WM_RBUTTONDBLCLK = &H206
Private Const WM_MBUTTONDOWN = &H207
Private Const WM_MBUTTONUP = &H208
Private Const WM_MBUTTONDBLCLK = &H209
Private Const WM_MOUSEWHEEL = &H20A
Private Const WH_JOURNALRECORD = 0
Type EVENTMSG
wMsg As Long
lParamLow As Long
lParamHigh As Long
' msgTime As Long
' hWndMsg As Long
End Type
Dim EMSG As EVENTMSG
Dim hHook As Long, frmHooked As Form, hFlags As Long
Public Function HookProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If nCode < 0 Then
HookProc = CallNextHookEx(hHook, nCode, wParam, lParam)
Exit Function
End If
Dim i%, j%
CopyMemory EMSG, ByVal lParam, Len(EMSG)
Select Case EMSG.wMsg
Case WM_KEYDOWN
If (hFlags And HFKeyDown) = HFKeyDown Then
If GetAsyncKeyState(vbKeyShift) Then j = 1
If GetAsyncKeyState(vbKeyControl) Then j = 2
If GetAsyncKeyState(vbKeyMenu) Then j = 4
If (EMSG.lParamLow And &HFF) > 31 Then
frmHooked.System_KeyDown MapVirtualKeyEx(EMSG.lParamLow And &HFF, 2, GetKeyboardLayout(GetWindowThreadProcessId(GetForegroundWindow, 0))), j
Else
frmHooked.System_KeyDown EMSG.lParamLow And &HFF, j
End If
End If
Case WM_KEYUP
If (hFlags And HFKeyUp) = HFKeyUp Then
If GetAsyncKeyState(vbKeyShift) Then j = 1
If GetAsyncKeyState(vbKeyControl) Then j = 2
If GetAsyncKeyState(vbKeyMenu) Then j = 4
If (EMSG.lParamLow And &HFF) > 31 Then
frmHooked.System_KeyUp MapVirtualKeyEx(EMSG.lParamLow And &HFF, 2, GetKeyboardLayout(GetWindowThreadProcessId(GetForegroundWindow, 0))), j
Else
frmHooked.System_KeyUp EMSG.lParamLow And &HFF, j
End If
End If
Case WM_MOUSEWHEEL
Debug.Print "MouseWheel"
Case WM_MOUSEMOVE
If (hFlags And HFMouseMove) = HFMouseMove Then
If GetAsyncKeyState(vbKeyLButton) Then i = 1
If GetAsyncKeyState(vbKeyRButton) Then i = 2
If GetAsyncKeyState(vbKeyMButton) Then i = 4
If GetAsyncKeyState(vbKeyShift) Then j = 1
If GetAsyncKeyState(vbKeyControl) Then j = 2
If GetAsyncKeyState(vbKeyMenu) Then j = 4
frmHooked.System_MouseMove i, j, CSng(EMSG.lParamLow), CSng(EMSG.lParamHigh)
End If
Case WM_LBUTTONDOWN, WM_RBUTTONDOWN, WM_MBUTTONDOWN
If (hFlags And HFMouseDown) = HFMouseDown Then
If GetAsyncKeyState(vbKeyShift) Then i = 1
If GetAsyncKeyState(vbKeyControl) Then i = 2
If GetAsyncKeyState(vbKeyMenu) Then i = 4
frmHooked.System_MouseDown 2 ^ ((EMSG.wMsg - 513) / 3), i, CSng(EMSG.lParamLow), CSng(EMSG.lParamHigh)
End If
Case WM_LBUTTONUP, WM_RBUTTONUP, WM_MBUTTONUP
If (hFlags And HFMouseUp) = HFMouseUp Then
If GetAsyncKeyState(vbKeyShift) Then i = 1
If GetAsyncKeyState(vbKeyControl) Then i = 2
If GetAsyncKeyState(vbKeyMenu) Then i = 4
frmHooked.System_MouseUp 2 ^ ((EMSG.wMsg - 514) / 3), i, CSng(EMSG.lParamLow), CSng(EMSG.lParamHigh)
End If
End Select
Call CallNextHookEx(hHook, nCode, wParam, lParam)
End Function
Public Sub SetHook(fOwner As Form, flags As HookFlags)
hHook = SetWindowsHookEx(WH_JOURNALRECORD, AddressOf HookProc, 0, 0)
Set frmHooked = fOwner
hFlags = flags
Window_SetAlwaysOnTop frmHooked.hwnd, True
End Sub
Public Sub RemoveHook()
UnhookWindowsHookEx hHook
Set frmHooked = Nothing
End Sub
Private Function Window_SetAlwaysOnTop(hwnd As Long, bAlwaysOnTop As Boolean) As Boolean
Window_SetAlwaysOnTop = SetWindowPos(hwnd, -2 - bAlwaysOnTop, 0, 0, 0, 0, SWP_NOREDRAW Or SWP_NOSIZE Or SWP_NOMOVE)
End Function
Sasha_karasov писал(а):Может, пригодится
Клавиатурный шпион
опубликовано: 08.07.2003 04:00
Я уверен, что каждый из нас сталкивался с задачей слежения за нажатием клавиш, но при этом использовались чьи-то готовые программы. На этот раз мы предлагаем Вам написать свою...
Реализовать клавиатурный шпион на Visual Basic очень легко. Для этого необходима функция
GetAsynckeyState, которая способна отслеживать нажатия клавиш, даже если форма в неактивном состоянии.
Наша программа будет фиксировать нажатия функциональных клавиш типа F1,CapsLock и т. д.. и записывать их в файл “C:\test.txt”.
Добавьте на форму текстовое поле Text2 и таймер Timer1 с интервалом 1.
Eduard писал(а):а что значит в примере фун-ция System_KeyDown у формы frmHooked её же нету у формы, а он в неё пытаеться чето посылать...
Option Explicit
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Dim Change As Boolean
Dim ChangeTo As Boolean
'Timer2.interval = 10
Private Sub Timer1_Timer()
Dim cnt As Long
Dim nAll As Boolean
For cnt = 0 To 225
If GetAsyncKeyState(cnt) < 0 Then
If cnt = 192 Then Change = True
End If
Next cnt
If Left(Command.Text, 1) = "`" Then Command.Text = Mid(Command.Text, 2)
If Right(Command.Text, 1) = "`" Then Command.Text = Mid(Command.Text, 1, Len(Command.Text) - 1)
End Sub
'Timer2.interval = 250
Private Sub Timer2_Timer()
If Change = True Then
ChangeTo = Not ChangeTo
If ChangeTo Then Me.Hide Else Me.Show
Change = False
End If
End Sub
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Function GetMessage Lib "user32" Alias "GetMessageA" (lpMsg As MSG, ByVal hwnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long) As Long
Private Const WM_KEYDOWN = &H100
Private Const WM_KEYUP = &H101
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
Dim WMSG As MSG
Dim MSGVAR As Long
Dim FLAG As Boolean
Sub main()
Do
Call GetMessage(WMSG, 0, 0, 0)
MSGVAR = WMSG.message
If MSGVAR = WM_KEYUP Then
FLAG = False
End If
If MSGVAR = WM_KEYDOWN Then
If Not FLAG Then
Debug.Print "KEY " & Chr(WMSG.wParam)
FLAG = True
End If
End If
Sleep 10
DoEvents
Loop
End Sub
Сейчас этот форум просматривают: Google-бот и гости: 173