3

How can I use GetKeyState to detect if the mouse's back or forward button is down when I receive a mouse event? MSDN's virtual key code list seems to define only left, right and middle mouse buttons.

Callback method that is passed to SetWindowsHookEx:

[MethodImpl(MethodImplOptions.NoInlining)]
private IntPtr LowLevelMouseProc(int nCode, UIntPtr wParam, IntPtr lParam)
{
    Debug.WriteLine(wParam.ToUInt32());
    Debug.WriteLine("MK_XBUTTON1: " + (wParam.ToUInt32() & 0x20));
    Debug.WriteLine("MK_XBUTTON2: " + (wParam.ToUInt32() & 0x40));
}

Outputs the following when either back or forward is pressed:

523
MK_XBUTTON1: 0
MK_XBUTTON2: 0
524
MK_XBUTTON1: 0
MK_XBUTTON2: 0
user1151923
  • 1,853
  • 6
  • 28
  • 44

2 Answers2

3

You were linking to the Windows CE documentation. The Desktop Windows documentation for Virtual-Key Codes contains the VK_XBUTTON1 and VK_XBUTTON2 codes. Those are the constants for the additional mouse buttons, that are often assigned to forward and backward navigation.

If you want to handle the X button messages immediately, they are posted to your application using WM_XBUTTONDOWN and WM_XBUTTONUP. The low-order word of wParam indicates which X button is down (if any).

IInspectable
  • 46,945
  • 8
  • 85
  • 181
  • ahh my bad with the wrong key code list... any idea how I would go about retrieving the low-order word in C#? – user1151923 Aug 25 '14 at 13:28
  • Masking out the low-order word would be `wParam & 0xff`. However, this is not necessary; it is enough to apply a bitwise AND with `MK_XBUTTON1` (0x20) or `MK_XBUTTON2` (0x40), and compare the result against zero. If the result is non-zero, the respective button(s) is pressed, otherwise it isn't. – IInspectable Aug 25 '14 at 14:23
  • hmm, does not seem to be working :/ I added my callback code to the question. – user1151923 Aug 25 '14 at 14:39
  • It appears instead of messing with the wParam I have to marshal lParam to a MSLLHOOKSTRUCT and check the mouseData field for values 0x10000 and 0x20000 as described [here](http://stackoverflow.com/questions/6371477/how-to-use-back-forward-navigation-button-events-in-wpf-webbrowser?rq=1) and [here](http://blogs.msdn.com/b/toub/archive/2006/05/03/589468.aspx). – user1151923 Aug 26 '14 at 07:15
  • I was referring to the `wParam` used in the messages (linked to in my answer). The `wParam` in your low level mouse hook proc is obviously different. Maybe you should thoroughly read the documentation for [LowLevelMouseProc](http://msdn.microsoft.com/en-us/library/windows/desktop/ms644986.aspx). – IInspectable Aug 26 '14 at 10:32
3

Assume you use C# WinForm, and what you want is: When the picture box is focused, detect if mouse's back/forward button was pressed.

private System.Windows.Forms.PictureBox picContent;
this.picContent.MouseDown += new System.Windows.Forms.MouseEventHandler(this.picContent_MouseDown);

private void picContent_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.XButton1)
        MessageBox.Show("Back");
    if (e.Button == MouseButtons.XButton2)
        MessageBox.Show("Forward");
}

Tested on .Net Framework 4.5.2

Neuron
  • 5,141
  • 5
  • 38
  • 59
123iamking
  • 2,387
  • 4
  • 36
  • 56