Index: src/win32/run.cxx
===================================================================
--- src/win32/run.cxx	(revision 8026)
+++ src/win32/run.cxx	(working copy)
@@ -1591,7 +1591,7 @@
     // Mask keeps the mouse button state:
     // Alt gets reported for the Alt-GR switch on foreign keyboards,
     // so we ignore it for WM_CHAR messages:
-    e_state = (e_state & 0xff000000) | shiftflags(uMsg==WM_CHAR);
+    e_state = (e_state & ANY_BUTTON) | shiftflags(uMsg==WM_CHAR);
     if (lParam & (1<<31)) { // key up events.
       e_is_click = (recent_keysym == e_keysym);
       recent_keysym = 0;
Index: src/osx/run.cxx
===================================================================
--- src/osx/run.cxx	(revision 8026)
+++ src/osx/run.cxx	(working copy)
@@ -690,7 +690,7 @@
   UInt32 chord;
   GetEventParameter( event, kEventParamMouseChord,
 		     typeUInt32, NULL, sizeof(UInt32), NULL, &chord );
-  e_state = ( e_state & 0xff0000 ) | state[ chord & 0x07 ];
+  e_state = ( e_state & ANY_BUTTON ) | state[ chord & 0x07 ];
 
   // make the stylus data produce something useful if there's no pen
   //if (no_stylus) {
@@ -792,7 +792,7 @@
   if ( mods & (controlKey|rightControlKey) ) state |= CTRL;
   if ( mods & (shiftKey|rightShiftKey) ) state |= SHIFT;
   if ( mods & alphaLock ) state |= CAPSLOCK;
-  e_state = ( e_state & 0xff000000 ) | state;
+  e_state = ( e_state & ANY_BUTTON ) | state;
   //printf( "State 0x%08x (%04x)\n", e_state, mods );
 }
 
Index: src/x11/run.cxx
===================================================================
--- src/x11/run.cxx	(revision 8026)
+++ src/x11/run.cxx	(working copy)
@@ -1217,7 +1217,12 @@
   e_x = xevent.xbutton.x;
   e_y_root = xevent.xbutton.y_root;
   e_y = xevent.xbutton.y;
-  e_state = (xevent.xbutton.state << 16) | extra_state;
+  // Only the 1st to the 13th bit in XButtonEvent::state are
+  // relevant to mouse buttons; the rest are a keymap mask,
+  // which can cause problems for FLTK's 6th, 7th and 8th mouse buttons.
+  // If we bitwise & the state member with XSTATE_MASK (defined in fltk/x.h)
+  // we remove the conflict.
+  e_state = ((xevent.xbutton.state & XSTATE_MASK) << 16) | extra_state;
   event_time = xevent.xbutton.time;
   // turn off is_click if enough time or mouse movement has passed:
   static int px, py;
@@ -1548,7 +1553,7 @@
 
   case EnterNotify:
     set_event_xy(false);
-    e_state = xevent.xcrossing.state << 16;
+    e_state = (xevent.xcrossing.state & XSTATE_MASK) << 16;
     if (xevent.xcrossing.detail == NotifyInferior) {event=MOVE; break;}
 //      printf("EnterNotify window %s, xmousewin %s\n",
 //	   window ? window->label() : "NULL",
@@ -1569,7 +1574,7 @@
 
   case LeaveNotify:
     set_event_xy(false);
-    e_state = xevent.xcrossing.state << 16;
+    e_state = (xevent.xcrossing.state & XSTATE_MASK) << 16;
     if (xevent.xcrossing.detail == NotifyInferior) {event=MOVE; break;}
 //      printf("LeaveNotify window %s, xmousewin %s\n",
 //	   window ? window->label() : "NULL",
Index: fltk/events.h
===================================================================
--- fltk/events.h	(revision 8026)
+++ fltk/events.h	(working copy)
@@ -180,7 +180,7 @@
   BUTTON1	= 0x01000000,	/*!< Left mouse button held down */
   BUTTON2	= 0x02000000,	/*!< Middle mouse button held down */
   BUTTON3	= 0x04000000,	/*!< Right mouse button held down */
-  ANY_BUTTON	= 0x7f000000, /*!< Any mouse button (up to 8) */
+  ANY_BUTTON	= 0xff000000, /*!< Any mouse button (up to 8) */
 #if defined(__APPLE__)
   ACCELERATOR	= CTRL,
   OPTION	= ALT,
Index: fltk/x11.h
===================================================================
--- fltk/x11.h	(revision 8026)
+++ fltk/x11.h	(working copy)
@@ -78,6 +78,7 @@
 #include "draw.h"
 
 extern FL_API Region	XRectangleRegion(int x, int y, int w, int h);
+#define XSTATE_MASK 0x1fff
 
 namespace fltk {
 

