Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
cbrnr opened this issue Nov 22, 2017 · 8 comments
Closed

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

cbrnr opened this issue Nov 22, 2017 · 8 comments
Milestone

Comments

@cbrnr
Copy link
Contributor

cbrnr commented Nov 22, 2017

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?

@dstansby
Copy link
Member

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

@dstansby dstansby added this to the v2.2 milestone Nov 22, 2017
@dstansby dstansby self-assigned this Nov 22, 2017
@cbrnr
Copy link
Contributor Author

cbrnr commented Nov 22, 2017

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

@dstansby
Copy link
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

@dstansby dstansby removed their assignment Nov 22, 2017
@dstansby
Copy link
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];
}

@cbrnr
Copy link
Contributor Author

cbrnr commented Nov 22, 2017

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.

@dopplershift
Copy link
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.

@jklymak
Copy link
Member

jklymak commented Nov 23, 2017

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

@cbrnr
Copy link
Contributor Author

cbrnr commented Nov 29, 2017

Closing since #9836 was merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants