Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

detect if mouse's back/forward button was pressed

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
like image 459
user1151923 Avatar asked Dec 16 '25 18:12

user1151923


2 Answers

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).

like image 66
IInspectable Avatar answered Dec 19 '25 13:12

IInspectable


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

like image 39
123iamking Avatar answered Dec 19 '25 13:12

123iamking



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!