Search code examples
c#mousekeyhook

Mouse Key Hook - Key listener not firing


I'm using the Global MouseKeyHook to listen, for both mouse clicks and key presses, whilst playing a video in a VLC activeX control. The mouse click listener triggers fine, and works as expected. But the key press/down listeners don't fire. I've attached some of the code below, any help would be appreciated.

Subscribe to the events

    public void playVideo(string videoPath, bool loop)
    {
        stopTriggered = false;

        this.loop = loop;
        this.videoPath = videoPath;
        this.WindowState = FormWindowState.Normal;
        this.BringToFront();

        m_GolbalHook = Hook.GlobalEvents();
        m_GolbalHook.MouseClick += m_GolbalHook_MouseClick;
        m_GolbalHook.KeyDown += m_GolbalHook_KeyDown;

        axVLCPlugin21.Focus();
        axVLCPlugin21.playlist.items.clear();
        axVLCPlugin21.playlist.add("file:///" + videoPath);
        axVLCPlugin21.AutoLoop = loop;
        axVLCPlugin21_Pos = ScreenPositons.Screen1_Start;

        axVLCPlugin21.playlist.play();
    }

Event triggered code

    private void m_GolbalHook_KeyDown(object sender, KeyEventArgs e)
    {
        //Not triggering at the moment
        switch (e.KeyCode)
        {
            case Keys.Right:
                shiftRight();
                break;
            case Keys.Left:
                shiftLeft();
                break;
        }
    }

Solution

  • Posting the solution I used, in case it helps others. All credit to Kilazur (see comments). It's not a perfect solution, but it works...

    m_GolbalHook = Hook.GlobalEvents();
    m_GolbalHook.MouseClick += m_GolbalHook_MouseClick;
    
    m_AppHook = Hook.AppEvents();
    m_AppHook.KeyDown += m_AppHook_KeyDown;
    

    I'm using Hook.GlobalEvents() for the mouse listener, and Hook.AppEvents() for the key listener.