Skip to content

Latest commit

History

34 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

WindowsInput

Capture and Simulate Keyboard and Mouse Input

WindowsInput provides simple .NET (C#) classes to capture and simulate Keyboard and mouse input using Win32's SetWindowsHook and SendInput. All of the interop is done for you and there is a simple programming model for everything.

NuGet nuget

Install-Package WindowsInput

Prerequisites

  • Windows: .Net 4.6.1+

Samples

Run Notepad using the Keyboard...

publicasyncTaskRunNotepad(){awaitWindowsInput.Simulate.Events()//Hold Windows Key+R.ClickChord(KeyCode.LWin,KeyCode.R).Wait(1000)//Type "notepad".Click("notepad").Wait(1000)//Press Enter.Click(KeyCode.Return).Wait(1000)//Type out our message..Click("These are your orders if you choose to accept them...").Click("This message will self destruct in 5 seconds.").Wait(5000)//Hold Alt+F4.ClickChord(KeyCode.Alt,KeyCode.F4).Wait(1000)//Press Tab then Enter..Click(KeyCode.Tab,KeyCode.Return)//Do it!.Invoke();

Capture Keys from the Keyboard and disable the 'a' key:

publicstaticvoidMain(){using(varKeyboard=WindowsInput.Capture.Global.KeyboardAsync()){//Capture all events from the keyboardKeyboard.KeyEvent+=Keyboard_KeyEvent;Console.ReadLine();}}privatestaticvoidKeyboard_KeyEvent(objectsender,EventSourceEventArgs<KeyboardEvent>e){if(e.Data?.KeyDown?.Key==WindowsInput.Events.KeyCode.A||e.Data?.KeyUp?.Key==WindowsInput.Events.KeyCode.A){e.Next_Hook_Enabled=false;}}

Text Snippet Replacement

This example waits for someone to type 'aaa' (three A's in a row) and then replaces the text with 'Always Ask Albert'.

publicstaticTaskDo(){using(varKeyboard=WindowsInput.Capture.Global.KeyboardAsync()){varListener=newWindowsInput.EventSources.TextSequenceEventSource(Keyboard,newWindowsInput.Events.TextClick("aaa"));Listener.Triggered+=(x,y)=>Listener_Triggered(Keyboard,x,y);;Listener.Enabled=true;Console.WriteLine("The keyboard is now listening for sequences. Try typing 'aaa' in notepad.");Console.WriteLine("Press enter to quit...");Console.ReadLine();}returnTask.CompletedTask;}privatestaticasyncvoidListener_Triggered(IKeyboardEventSourceKeyboard,objectsender,WindowsInput.EventSources.TextSequenceEventArgse){e.Input.Next_Hook_Enabled=false;varToSend=WindowsInput.Simulate.Events();for(inti=1;i<e.Sequence.Text.Length;i++){ToSend.Click(WindowsInput.Events.KeyCode.Backspace);}ToSend.Click("Always ask albert!");//We suspend keyboard events because we don't want to accidently trigger a recursive loop if our//sending text actually had 'aaa' in it.using(Keyboard.Suspend()){awaitToSend.Invoke();}}

Trigger text on a special key combination

publicstaticTaskDo(){using(varKeyboard=WindowsInput.Capture.Global.KeyboardAsync()){varListener=newWindowsInput.EventSources.KeyChordEventSource(Keyboard,newChordClick(KeyCode.Control,KeyCode.Alt,KeyCode.Shift));Listener.Triggered+=(x,y)=>Listener_Triggered(Keyboard,x,y);Listener.Reset_On_Parent_EnabledChanged=false;Listener.Enabled=true;Console.WriteLine("The keyboard is now listening for chords. Try typing 'CONTROL+ALT+SHIFT' in notepad.");Console.WriteLine("Press enter to quit...");Console.ReadLine();}returnTask.CompletedTask;}privatestaticasyncvoidListener_Triggered(IKeyboardEventSourceKeyboard,objectsender,WindowsInput.EventSources.KeyChordEventArgse){varToSend=WindowsInput.Simulate.Events();ToSend.Click("You pressed the magic keys.");using(Keyboard.Suspend()){awaitToSend.Invoke();}}

Credits


This work is a tightly unified library that was built on the backs of the following giants:

About

Capture and Simulate Keyboard and Mouse Input

Resources

Stars

131 stars

Watchers

7 watching

Forks

Releases

Packages

Used by

Contributors

Languages