public MainForm()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // Add any constructor code after InitializeComponent call
            //
            SetInitialMessage();

            // EXAMPLE CODE SECTION
            keyboardHook          = HookFactory.CreateKeyboardHookExt();
            keyboardHook.KeyDown += new KeyboardEventHandlerExt(keyboardHook_KeyDown);

            //
            // Note: Use IMouseHookExt for mouse move events.
            //
            mouseHook = HookFactory.CreateMouseHookLite();
            mouseHook.LeftButtonDown += new MouseEventHandlerExt(mouseHook_LeftButtonDown);
            mouseHook.LeftButtonUp   += new MouseEventHandlerExt(mouseHook_LeftButtonUp);
            mouseHook.MouseWheel     += new MouseEventHandlerExt(mouseHook_MouseWheel);

            UpdateEnabledStates();
        }
 public void DefaultCreationTest(TestMethodRecord tmr)
 {
     using (IKeyboardHookExt keyboard = HookFactory.CreateKeyboardHookExt())
     {
         tmr.RunTest(keyboard != null, "Keyboard hook created.");
     }
 }
        public void ReferenceCountingTest(TestMethodRecord tmr)
        {
            //
            // In this test we are observing how the various keyboard hook
            // classes are interacting with their implementation classes. They
            // should add references in their constructor and release them when
            // disposed. The implementation should only be disposed when the ref
            // count is zero.
            //
            KeyboardHookTestImpl testImpl = new KeyboardHookTestImpl();

            tmr.RunTest(testImpl.ReferenceCount == 0, "Now references count is 0.");
            tmr.RunTest(testImpl.Disposed == false, "Not initially disposed.");

            using (IKeyboardHook hook1 = HookFactory.CreateKeyboardHook(testImpl))
            {
                tmr.RunTest(testImpl.ReferenceCount == 1, "Now references count is 1.");
                tmr.RunTest(testImpl.Disposed == false, "Not disposed.");

                using (IKeyboardHookExt hook2 = HookFactory.CreateKeyboardHookExt(testImpl))
                {
                    tmr.RunTest(testImpl.ReferenceCount == 2, "Now references count is 2.");
                    tmr.RunTest(testImpl.Disposed == false, "Not disposed.");
                }

                tmr.RunTest(testImpl.ReferenceCount == 1, "Now references count is 1.");
                tmr.RunTest(testImpl.Disposed == false, "Not disposed.");
            }

            tmr.RunTest(testImpl.ReferenceCount == 0, "Now references count is 0.");
            tmr.RunTest(testImpl.Disposed == true, "Disposed.");
        }
Exemple #4
0
        public void CreateKeyboardHookTests(TestMethodRecord tmr)
        {
            using (IKeyboardHook hook = HookFactory.CreateKeyboardHook())
            {
                tmr.RunTest(hook != null, "IKeyboardHook created successfully with default options.");
            }

            using (IKeyboardHookExt hookExt = HookFactory.CreateKeyboardHookExt())
            {
                tmr.RunTest(hookExt != null, "IKeyboardHookExt created successfully with default options.");
            }
        }
        public void InstallHookTests(TestMethodRecord tmr)
        {
            using (IKeyboardHook kHook = HookFactory.CreateKeyboardHook())
            {
                TestInstallHook(tmr, kHook, "IKeyboardHook");
            }

            using (IKeyboardHookExt kHookExt = HookFactory.CreateKeyboardHookExt())
            {
                TestInstallHook(tmr, kHookExt, "IKeyboardHookExt");
            }
        }
        public void EventTest(TestMethodRecord tmr)
        {
            KeyboardHookEventArgs[] keyArgs = GenerateRandomKeyData();

            KeyboardHookTestImpl hookImpl = new KeyboardHookTestImpl();

            using (IKeyboardHookExt keyboard = HookFactory.CreateKeyboardHookExt(hookImpl))
            {
                keyboard.InstallHook();

                keyboard.KeyDown += new KeyboardEventHandlerExt(EventTest_KeyDown);

                //
                // Trigger a bunch of keyboard events. The event handler will
                // capture the output and we will then compare it here.
                //
                foreach (KeyboardHookEventArgs keyArg in keyArgs)
                {
                    hookImpl.TriggerKeyAction(KeyboardEvents.KeyDown, keyArg);
                }
            }

            tmr.RunTest(keyArgs.Length == eventTest_ArgsList.Count, "The proper number of events were caught.");

            bool same = true;

            for (int i = 0; i < keyArgs.Length; i++)
            {
                KeyboardHookEventArgs kea1 = keyArgs[i];
                KeyboardHookEventArgs kea2 = (KeyboardHookEventArgs)eventTest_ArgsList[i];

                if (!CompareKeyArgs(kea1, kea2))
                {
                    tmr.WriteLine("Failed event results comparison at index " + i);
                    same = false;
                    break;
                }
            }

            tmr.RunTest(same, "The event callback captured all the events successfully.");
        }
        public void SortedEventTest(TestMethodRecord tmr)
        {
            KeyboardHookTestImpl hookImpl = new KeyboardHookTestImpl();
            IKeyboardHookExt     keyboard = HookFactory.CreateKeyboardHookExt(hookImpl);

            keyboard.InstallHook();

            keyboard.KeyDown       += new KeyboardEventHandlerExt(SortedEventTest_KeyDown);
            keyboard.KeyUp         += new KeyboardEventHandlerExt(SortedEventTest_KeyUp);
            keyboard.SystemKeyUp   += new KeyboardEventHandlerExt(SortedEventTest_SystemKeyUp);
            keyboard.SystemKeyDown += new KeyboardEventHandlerExt(SortedEventTest_SystemKeyDownKeyUp);

            KeyboardHookEventArgs[] keyArgs = GenerateRandomKeyData();

            KeyboardHookEventArgs kea1 = keyArgs[0];
            KeyboardHookEventArgs kea2 = keyArgs[1];
            KeyboardHookEventArgs kea3 = keyArgs[2];
            KeyboardHookEventArgs kea4 = keyArgs[3];

            hookImpl.TriggerKeyAction(KeyboardEvents.KeyDown, kea1);
            hookImpl.TriggerKeyAction(KeyboardEvents.KeyUp, kea2);
            hookImpl.TriggerKeyAction(KeyboardEvents.SystemKeyDown, kea3);
            hookImpl.TriggerKeyAction(KeyboardEvents.SystemKeyUp, kea4);

            tmr.RunTest(sortedEventTest_ArgsListDown.Count == 1, "One KeyDown event trigged.");
            tmr.RunTest(sortedEventTest_ArgsListUp.Count == 1, "One KeyUp event trigged.");
            tmr.RunTest(sortedEventTest_ArgsListSysDown.Count == 1, "One SystemKeyDown event trigged.");
            tmr.RunTest(sortedEventTest_ArgsListSysUp.Count == 1, "One SystemKeyUp event trigged.");

            KeyboardHookEventArgs keaB1 = (KeyboardHookEventArgs)sortedEventTest_ArgsListDown[0];
            KeyboardHookEventArgs keaB2 = (KeyboardHookEventArgs)sortedEventTest_ArgsListUp[0];
            KeyboardHookEventArgs keaB3 = (KeyboardHookEventArgs)sortedEventTest_ArgsListSysDown[0];
            KeyboardHookEventArgs keaB4 = (KeyboardHookEventArgs)sortedEventTest_ArgsListSysUp[0];

            tmr.RunTest(CompareKeyArgs(kea1, keaB1), "Correct KeyDown event caught.");
            tmr.RunTest(CompareKeyArgs(kea2, keaB2), "Correct KeyUp event caught.");
            tmr.RunTest(CompareKeyArgs(kea3, keaB3), "Correct SysKeyDown event caught.");
            tmr.RunTest(CompareKeyArgs(kea4, keaB4), "Correct SysKeyUp event caught.");
        }
 public void NullImplementationTest(TestMethodRecord tmr)
 {
     tmr.RegisterException("Null implementation causes an exception.", typeof(ArgumentNullException));
     IKeyboardHookExt keyboard = HookFactory.CreateKeyboardHookExt(null);
 }