Skip to content

Shift+Arrow key events not detected in osx backend #9835

Closed

Description

@cbrnr

Consider the following example code:

import matplotlib.pyplot as plt
fig = plt.figure()

def on_key(event):
    print('you pressed', event.key, event.xdata, event.ydata)

fig.canvas.mpl_connect('key_press_event', on_key)

Now when I run this code and press Shift+Left Arrow, the qt5 backend correctly detects this event as shift+left. However, the osx backend (which AFAIK is the default backend on macOS) doesn't register the shift key at all, I only get left. Is it possible to fix this somehow?

Activity

  1. dstansby commented on Nov 22, 2017

    @dstansby
    Member

    I delved into the key handling recently, I'll have a look at see if I can do anything

  2. added this to the milestone on Nov 22, 2017
  3. self-assigned this
    on Nov 22, 2017
  4. cbrnr commented on Nov 22, 2017

    @cbrnr
    ContributorAuthor

    Awesome! Let me know if I can be of any help!

  5. dstansby commented on Nov 22, 2017

    @dstansby
    Member

    I've tracked it back to the OSX c code, which converts whatever the key press event is to a char here:

    const char* s = [self convertKeyEvent: event];

    Unfortunately I don't really do C, so someone else is going to have to figure it out from here

  6. removed their assignment
    on Nov 22, 2017
  7. dstansby commented on Nov 22, 2017

    @dstansby
    Member

    And I'm guessing this is the function that needs to be modified somehow:

    matplotlib/src/_macosx.m

    Lines 2558 to 2617 in 813d842

    - (const char*)convertKeyEvent:(NSEvent*)event
    {
    NSDictionary* specialkeymappings = [NSDictionary dictionaryWithObjectsAndKeys:
    @"left", [NSNumber numberWithUnsignedLong:NSLeftArrowFunctionKey],
    @"right", [NSNumber numberWithUnsignedLong:NSRightArrowFunctionKey],
    @"up", [NSNumber numberWithUnsignedLong:NSUpArrowFunctionKey],
    @"down", [NSNumber numberWithUnsignedLong:NSDownArrowFunctionKey],
    @"f1", [NSNumber numberWithUnsignedLong:NSF1FunctionKey],
    @"f2", [NSNumber numberWithUnsignedLong:NSF2FunctionKey],
    @"f3", [NSNumber numberWithUnsignedLong:NSF3FunctionKey],
    @"f4", [NSNumber numberWithUnsignedLong:NSF4FunctionKey],
    @"f5", [NSNumber numberWithUnsignedLong:NSF5FunctionKey],
    @"f6", [NSNumber numberWithUnsignedLong:NSF6FunctionKey],
    @"f7", [NSNumber numberWithUnsignedLong:NSF7FunctionKey],
    @"f8", [NSNumber numberWithUnsignedLong:NSF8FunctionKey],
    @"f9", [NSNumber numberWithUnsignedLong:NSF9FunctionKey],
    @"f10", [NSNumber numberWithUnsignedLong:NSF10FunctionKey],
    @"f11", [NSNumber numberWithUnsignedLong:NSF11FunctionKey],
    @"f12", [NSNumber numberWithUnsignedLong:NSF12FunctionKey],
    @"f13", [NSNumber numberWithUnsignedLong:NSF13FunctionKey],
    @"f14", [NSNumber numberWithUnsignedLong:NSF14FunctionKey],
    @"f15", [NSNumber numberWithUnsignedLong:NSF15FunctionKey],
    @"f16", [NSNumber numberWithUnsignedLong:NSF16FunctionKey],
    @"f17", [NSNumber numberWithUnsignedLong:NSF17FunctionKey],
    @"f18", [NSNumber numberWithUnsignedLong:NSF18FunctionKey],
    @"f19", [NSNumber numberWithUnsignedLong:NSF19FunctionKey],
    @"scroll_lock", [NSNumber numberWithUnsignedLong:NSScrollLockFunctionKey],
    @"break", [NSNumber numberWithUnsignedLong:NSBreakFunctionKey],
    @"insert", [NSNumber numberWithUnsignedLong:NSInsertFunctionKey],
    @"delete", [NSNumber numberWithUnsignedLong:NSDeleteFunctionKey],
    @"home", [NSNumber numberWithUnsignedLong:NSHomeFunctionKey],
    @"end", [NSNumber numberWithUnsignedLong:NSEndFunctionKey],
    @"pagedown", [NSNumber numberWithUnsignedLong:NSPageDownFunctionKey],
    @"pageup", [NSNumber numberWithUnsignedLong:NSPageUpFunctionKey],
    @"backspace", [NSNumber numberWithUnsignedLong:NSDeleteCharacter],
    @"enter", [NSNumber numberWithUnsignedLong:NSEnterCharacter],
    @"tab", [NSNumber numberWithUnsignedLong:NSTabCharacter],
    @"enter", [NSNumber numberWithUnsignedLong:NSCarriageReturnCharacter],
    @"backtab", [NSNumber numberWithUnsignedLong:NSBackTabCharacter],
    @"escape", [NSNumber numberWithUnsignedLong:27],
    nil
    ];
    NSMutableString* returnkey = [NSMutableString string];
    if ([event modifierFlags] & NSControlKeyMask)
    [returnkey appendString:@"ctrl+" ];
    if ([event modifierFlags] & NSAlternateKeyMask)
    [returnkey appendString:@"alt+" ];
    if ([event modifierFlags] & NSCommandKeyMask)
    [returnkey appendString:@"cmd+" ];
    unichar uc = [[event charactersIgnoringModifiers] characterAtIndex:0];
    NSString* specialchar = [specialkeymappings objectForKey:[NSNumber numberWithUnsignedLong:uc]];
    if (specialchar)
    [returnkey appendString:specialchar];
    else
    [returnkey appendString:[event charactersIgnoringModifiers]];
    return [returnkey UTF8String];
    }

  8. cbrnr commented on Nov 22, 2017

    @cbrnr
    ContributorAuthor

    This looks promising. I wonder if it as easy as adding

    if ([event modifierFlags] & NSShiftKeyMask)
            [returnkey appendString:@"shift+" ];
    

    in line 2608... but I guess this needs to be restricted to only a subset of keys such as the arrow keys.

  9. dopplershift commented on Nov 22, 2017

    @dopplershift
    Contributor

    Adding that for me and rebuilding does get me shift events, but as you expected, shift + g produces "shift+G". It looks like the Qt backend has a list of special keys it looks for first, then it assumes characters and removes the shift modifier.

  10. jklymak commented on Nov 23, 2017

    @jklymak
    Member

    @cbrnr Have a look at #9836 if you are able to download development versions....

  11. cbrnr commented on Nov 29, 2017

    @cbrnr
    ContributorAuthor

    Closing since #9836 was merged.

  12. modified the milestones: , v2.2.0 on Feb 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions