@@ -25,56 +25,54 @@ Install by [nuget](https://www.nuget.org/packages/EventHook)
2525## Sample Code:
2626
2727``` csharp
28- var eventHookFactory = new EventHookFactory ();
29-
30- var keyboardWatcher = eventHookFactory .GetKeyboardWatcher ();
31- keyboardWatcher .Start ();
32- keyboardWatcher .OnKeyInput += (s , e ) =>
33- {
34- Console .WriteLine (string .Format (" Key {0} event of key {1}" , e .KeyData .EventType , e .KeyData .Keyname ));
35- };
36-
37- var mouseWatcher = eventHookFactory .GetMouseWatcher ();
38- mouseWatcher .Start ();
39- mouseWatcher .OnMouseInput += (s , e ) =>
28+ using (var eventHookFactory = new EventHookFactory ())
4029{
41- Console .WriteLine (string .Format (" Mouse event {0} at point {1},{2}" , e .Message .ToString (), e .Point .x , e .Point .y ));
42- };
43-
44- var clipboardWatcher = eventHookFactory .GetClipboardWatcher ();
45- clipboardWatcher .Start ();
46- clipboardWatcher .OnClipboardModified += (s , e ) =>
47- {
48- Console .WriteLine (string .Format (" Clipboard updated with data '{0}' of format {1}" , e .Data , e .DataFormat .ToString ()));
49- };
50-
51-
52- var applicationWatcher = eventHookFactory .GetApplicationWatcher ();
53- applicationWatcher .Start ();
54- applicationWatcher .OnApplicationWindowChange += (s , e ) =>
55- {
56- Console .WriteLine (string .Format (" Application window of '{0}' with the title '{1}' was {2}" , e .ApplicationData .AppName , e .ApplicationData .AppTitle , e .Event ));
57- };
58-
59- var printWatcher = eventHookFactory .GetPrintWatcher ();
60- printWatcher .Start ();
61- printWatcher .OnPrintEvent += (s , e ) =>
62- {
63- Console .WriteLine (string .Format (" Printer '{0}' currently printing {1} pages." , e .EventData .PrinterName , e .EventData .Pages ));
64- };
65-
66- // waiting here to keep this thread running
67- Console .Read ();
68-
69- // stop watching
70- keyboardWatcher .Stop ();
71- mouseWatcher .Stop ();
72- clipboardWatcher .Stop ();
73- applicationWatcher .Stop ();
74- printWatcher .Stop ();
75-
76- // dispose
77- eventHookFactory .Dispose ();
30+ var keyboardWatcher = eventHookFactory .GetKeyboardWatcher ();
31+ keyboardWatcher .Start ();
32+ keyboardWatcher .OnKeyInput += (s , e ) =>
33+ {
34+ Console .WriteLine (string .Format (" Key {0} event of key {1}" , e .KeyData .EventType , e .KeyData .Keyname ));
35+ };
36+
37+ var mouseWatcher = eventHookFactory .GetMouseWatcher ();
38+ mouseWatcher .Start ();
39+ mouseWatcher .OnMouseInput += (s , e ) =>
40+ {
41+ Console .WriteLine (string .Format (" Mouse event {0} at point {1},{2}" , e .Message .ToString (), e .Point .x , e .Point .y ));
42+ };
43+
44+ var clipboardWatcher = eventHookFactory .GetClipboardWatcher ();
45+ clipboardWatcher .Start ();
46+ clipboardWatcher .OnClipboardModified += (s , e ) =>
47+ {
48+ Console .WriteLine (string .Format (" Clipboard updated with data '{0}' of format {1}" , e .Data , e .DataFormat .ToString ()));
49+ };
50+
51+
52+ var applicationWatcher = eventHookFactory .GetApplicationWatcher ();
53+ applicationWatcher .Start ();
54+ applicationWatcher .OnApplicationWindowChange += (s , e ) =>
55+ {
56+ Console .WriteLine (string .Format (" Application window of '{0}' with the title '{1}' was {2}" , e .ApplicationData .AppName , e .ApplicationData .AppTitle , e .Event ));
57+ };
58+
59+ var printWatcher = eventHookFactory .GetPrintWatcher ();
60+ printWatcher .Start ();
61+ printWatcher .OnPrintEvent += (s , e ) =>
62+ {
63+ Console .WriteLine (string .Format (" Printer '{0}' currently printing {1} pages." , e .EventData .PrinterName , e .EventData .Pages ));
64+ };
65+
66+ // waiting here to keep this thread running
67+ Console .Read ();
68+
69+ // stop watching
70+ keyboardWatcher .Stop ();
71+ mouseWatcher .Stop ();
72+ clipboardWatcher .Stop ();
73+ applicationWatcher .Stop ();
74+ printWatcher .Stop ();
75+ }
7876```
7977
8078![ alt tag] ( https://raw.githubusercontent.com/justcoding121/Windows-User-Action-Hook/stable/EventHook.Examples/EventHook.ConsoleApp.Example/Capture.PNG )
0 commit comments