From 44f37c836c552554a1ac362bc22342c3b740ab3f Mon Sep 17 00:00:00 2001
From: Timur Pocheptsov <Timur.Pocheptsov@cern.ch>
Date: Tue, 20 Nov 2012 14:50:04 +0000
Subject: [PATCH] It looks like keyboard events are processed incorrectly.

Several fixed (not finished yet):

1. Map ascii symbols to themselves.
2. Mask keymodifiers (otherwise there are some non zero flags even if no modifier key is pressed).
3. Looks like not every map-raised window receives focus - as this is never described, I guess
   a window with override redirect does not receive a focus when raised (otherwise menu keybindings would
   never work in ROOT).


git-svn-id: http://root.cern.ch/svn/root/trunk@47478 27541ba8-7e3a-0410-8455-c3a389f83636
---
 graf2d/cocoa/src/QuartzWindow.mm | 39 +++++++++++++++++++-------------
 graf2d/cocoa/src/X11Events.mm    | 10 +++++---
 2 files changed, 30 insertions(+), 19 deletions(-)

diff --git a/graf2d/cocoa/src/QuartzWindow.mm b/graf2d/cocoa/src/QuartzWindow.mm
index a6d6b872276..94a07e1c29d 100644
--- a/graf2d/cocoa/src/QuartzWindow.mm
+++ b/graf2d/cocoa/src/QuartzWindow.mm
@@ -2215,7 +2215,7 @@ void print_mask_info(ULong_t mask)
 - (void) addPassiveKeyGrab : (unichar) keyCode modifiers : (NSUInteger) modifiers
 {
    //Remove and add (not to traverse twice).
-   [self removePassiveKeyGrab : keyCode modifiers : modifiers];
+ //  [self removePassiveKeyGrab : keyCode modifiers : modifiers];
    PassiveKeyGrab * const newGrab = [[PassiveKeyGrab alloc] initWithKey : keyCode modifiers : modifiers];
    [fPassiveKeyGrabs addObject : newGrab];
    [newGrab release];
@@ -2561,9 +2561,14 @@ void print_mask_info(ULong_t mask)
 }
 
 //______________________________________________________________________________
-- (void) keyUp:(NSEvent *)theEvent
+- (void) keyUp : (NSEvent *) theEvent
 {
-   (void)theEvent;
+   assert(fID != 0 && "keyUp, fID is 0");
+
+   TGCocoa * const vx = dynamic_cast<TGCocoa *>(gVirtualX);
+   assert(vx != 0 && "keyUp, gVirtualX is null or not of TGCocoa type");
+   vx->GetEventTranslator()->GenerateKeyReleaseEvent(self, theEvent);
+
 }
 
 //First responder staff.
@@ -2584,22 +2589,24 @@ void print_mask_info(ULong_t mask)
 //______________________________________________________________________________
 - (BOOL) becomeFirstResponder
 {
-   //Change focus.
-   NSView<X11Window> *focusView = nil;
-   for (NSView<X11Window> *view = self; view; view = view.fParentView) {
-      if (view.fEventMask & kFocusChangeMask) {
-         focusView = view;
-         break;
+   if (!fOverrideRedirect) {
+      //Change focus.
+      NSView<X11Window> *focusView = nil;
+      for (NSView<X11Window> *view = self; view; view = view.fParentView) {
+         if (view.fEventMask & kFocusChangeMask) {
+            focusView = view;
+            break;
+         }
       }
-   }
 
-   if (!focusView)
-      focusView = ((QuartzWindow *)[self window]).fContentView;
+      if (!focusView)
+         focusView = ((QuartzWindow *)[self window]).fContentView;
+      
+      TGCocoa * const vx = dynamic_cast<TGCocoa *>(gVirtualX);
+      assert(vx != 0 && "becomeFirstResponder, gVirtualX is null or not of TGCocoa type");
+      vx->GetEventTranslator()->GenerateFocusChangeEvent(focusView);
+   }
    
-   TGCocoa * const vx = dynamic_cast<TGCocoa *>(gVirtualX);
-   assert(vx != 0 && "becomeFirstResponder, gVirtualX is null or not of TGCocoa type");
-   vx->GetEventTranslator()->GenerateFocusChangeEvent(focusView);
-
    return YES;
 }
 
diff --git a/graf2d/cocoa/src/X11Events.mm b/graf2d/cocoa/src/X11Events.mm
index 87639222db3..7f21b0efbad 100644
--- a/graf2d/cocoa/src/X11Events.mm
+++ b/graf2d/cocoa/src/X11Events.mm
@@ -179,6 +179,10 @@ void MapUnicharToKeySym(unichar key, char *buf, Int_t /*len*/, UInt_t &rootKeySy
 //______________________________________________________________________________
 Int_t MapKeySymToKeyCode(Int_t keySym)
 {
+   //Apart from special keys, ROOT has also ASCII symbols, they map directly to themselves.
+   if (keySym >= 0x20 && keySym <= 0x7e)
+      return keySym;
+
    static const KeySymPair<EKeySym, unichar> keyMap[] = {
       {kKey_Escape, 27},
       {kKey_Tab, NSTabCharacter},
@@ -1677,7 +1681,7 @@ void EventTranslator::GenerateKeyReleaseEventNoGrab(NSEvent *theEvent)
 
    if (candidateView && Detail::IsParent(fFocusView, candidateView))
       GenerateKeyEventForView(candidateView, theEvent);
-   else 
+   else
       GenerateKeyEventForView(fFocusView, theEvent);
 }
 
@@ -1702,7 +1706,7 @@ void EventTranslator::GenerateKeyEventForView(NSView<X11Window> *view, NSEvent *
       childView = view.isHidden ? nil : view;
       view = view.fParentView;
    }
-      
+ 
    NSPoint mousePosition = {};
    if (QuartzWindow * const topLevel = FindWindowUnderPointer())
       mousePosition = [topLevel mouseLocationOutsideOfEventStream];
@@ -1770,7 +1774,7 @@ void EventTranslator::FindKeyGrabView(NSView<X11Window> *fromView, NSEvent *theE
    assert(characters != nil && "FindKeyGrabView, [theEvent characters] returned nil");
    assert([characters length] > 0 && "FindKeyGrabView, characters is an empty string");
 
-   const NSUInteger modifiers = [theEvent modifierFlags];
+   const NSUInteger modifiers = [theEvent modifierFlags] & NSDeviceIndependentModifierFlagsMask;
    const unichar keyCode = [characters characterAtIndex : 0];
 
    for (NSView<X11Window> *v = fromView; v; v = v.fParentView) {
-- 
GitLab