How to get a keypress event in an console application

U

Ulrich Kotzolt

I use to get the function keys that were pressed in an console
application cause this application should also be used over a telnet
server.
Does someone know how to get the keys.
I already thought about p-invoke, but which function do i need and were
can i get a reference of user32.dll
Thanks for your help in advance.
 
L

LOZANO-MORÁN, Gabriel

Here is a sample of a key logger I created in VB6, I needed to log all keys
being pressed to test if a keyboard was malfunctioning. If I had more time I
could translate it to C#. But I am sure that this will get you started. It
would be a nice exercise though :)

There is also an article in the Microsoft Knowledge Base titled "HOW TO: Set
a Windows Hook in Visual C# .NET"

'===========================================================================
=
' Keylogger
'===========================================================================
=
' Dit toolke dient om te registreren welke toetsen ingedrukt worden. De
' applicatie kan draaien op de achtergrond.
'===========================================================================
=

'===========================================================================
=
' Revisies
'===========================================================================
=
' Versie : v1.0.0
' Auteur : Gabriel Lozano-Morán
' Datum : 12-08-2004
' PID : 6128
' Opmerkingen : Initiële versie
'===========================================================================
=

' API Functies
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 UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Long) As Long

Private Declare Function CallNextHookEx Lib "user32" _
(ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, _
lParam As Any) As Long

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(pDst As Any, pSrc As Any, ByVal ByteLen As Long)

Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" _
(ByVal ncode As Long, ByVal lMapType As Long) As Long

Private Declare Sub GetLocalTime Lib "kernel32" (lpSystemTime As SYSTEMTIME)

Public Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA"
(ByVal lpBuffer As String, nSize As Long) As Long
Public Declare Function GetComputerName Lib "kernel32" Alias
"GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long

' Constanten
Private Const WH_KEYBOARD_LL = 13&
Private Const WM_KEYDOWN = &H100

' Structuren
Private Type KBDLLHOOKSTRUCT
code As Long
End Type

Private Type SYSTEMTIME
Year As Integer
Month As Integer
DayOfWeek As Integer
Day As Integer
Hour As Integer
Minute As Integer
Second As Integer
Milliseconds As Integer
End Type


' Variabelen
Dim hook As Long, keyCode As Long
Dim hookKey As KBDLLHOOKSTRUCT
Dim LocalTime As SYSTEMTIME
Dim dtLocalTime As String, strCharacter As String
Public strUser As String, strComputer As String, keyList(255) As String


Public Function KeyboardProc(ByVal ncode As Long, ByVal wParam As Long,
ByVal lParam As Long) As Long
' Kontroleer of toets ingedrukt is
If wParam = WM_KEYDOWN Then

' Kopie van geheugenblok lParam naar hookKey
Call CopyMemory(hookKey, ByVal lParam, Len(hookKey))
keyCode = hookKey.code

If keyCode < 256 Then
' Enkel wanneer de ASCII waarde kleiner is dan 256
' Om één of andere reden krijg ik waarden > dan 255 wanneer
' een niet numerieke of alfanumerieke toets ingedrukt wordt

' Ophalen van de tijd met milisecondes
' Zoals gebeurt in Datacaptatie bij de registratie van de
metingen
GetLocalTime LocalTime
With LocalTime
dtLocalTime = Format(.Day, "00") & "-" & Format(.Month,
"00") & _
"-" & Format(.Year, "0000") & " "
dtLocalTime = dtLocalTime & Format(.Hour, "00") & ":" &
Format(.Minute, "00") & _
":" & Format(.Second, "00") & "." &
Format(.Milliseconds, "000")
End With

' Ophalen van de karakter
strCharacter = keyList(keyCode)
If Len(strCharacter) = 0 Then
If keyCode > 0 Then
strCharacter = Chr(MapVirtualKey(keyCode, 2)) ' Vertaal
de ASCII waarde naar de juiste key, afhankelijk van de keyboard layout
Else
strCharachter = "0"
End If
End If
strCharacter = "[" & strCharacter & "]"

' Wegschrijvan naar de log
frmMain.txtKeylog.Text = frmMain.txtKeylog.Text & dtLocalTime &
_
vbTab & strUser & vbTab & strComputer &
vbTab & _
keyCode & vbTab & strCharacter & vbCrLf
End If
End If

' Volgende hook in de ketting
KeyboardProc = CallNextHookEx(hook, ncode, wParam, lParam)
End Function

Public Function KeyboardHook()

' Luisteren naar keyboard events
hook = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf KeyboardProc,
App.hInstance, 0&)

End Function


Public Function Unhook()
' Stoppen met luisteren naar keyboard events
Call UnhookWindowsHookEx(hook)
hook = 0
Unhook = 1
End Function
 
G

Gabriel Lozano-Morán

I found this on the net, keyboard hooking in C#:

http://www.ms-inc.net/quickapps.aspx?ProductID=KeyHook

Gabriel Lozano-Morán
Software Engineer
Sogeti

I use to get the function keys that were pressed in an console application
cause this application should also be used over a telnet server.
Does someone know how to get the keys.
I already thought about p-invoke, but which function do i need and were can
i get a reference of user32.dll
Thanks for your help in advance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top