8000 Qt4 keys by mrterry · Pull Request #2273 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Qt4 keys #2273

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
wants to merge 20 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
simplify key getting logic
  • Loading branch information
mrterry committed Sep 19, 2013
commit b80a8e7485e09f82c4c8f8b3e7255cac419d67b1
40 changes: 13 additions & 27 deletions lib/matplotlib/backends/backend_qt4.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,39 +341,25 @@ def _get_key(self, event):
return None

event_key = event.key()
event_mods = int(event.modifiers())
event_mods = int(event.modifiers()) # actually a bitmask

mods = [p for p, m, k in MODIFIER_KEYS
if event_key != k and event_mods & m == m]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor point: is the == m part redundant?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm cautious of making a statement on operator precedence, but I think a pair of parentheses would be helpful here:

[event_key != k and (event_mods & m) == m]

if I'm not mistaken?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bit-wise operators have very high precedence, but the parens do make it easier to read. i'll add them.

try:
# for certain keys (enter, left, backspace, etc) use a word for the
# key, rather than unicode
key = SPECIAL_KEYS[event_key]
except KeyError:
# for easy cases, event.text() handles caps lock and capitalization
# for us
key = unicode(event.text())

# if modifier (ctrl, alt, super) keys are being pressed,
# event.text() will be the empty string. QT always gives upper case
# letters, so we have to read from shift and lower() manually.
# Finally, since it is not possible to get the CapsLock state we
# cannot accurately compute the case of a pressed key when
# ctrl+shift+p is pressed.
if key == '':
# python may barf if event_key > 0x10000
try:
key = unichr(event_key)
762C except ValueError:
return None

# if shift is not being pressed, lower() it (CapsLock is
# ignored)
if not event_mods & QtCore.Qt.ShiftModifier:
key = key.lower()

for prefix, modifier, Qt_key in MODIFIER_KEYS:
if event_key != Qt_key and event_mods & modifier == modifier:
key = u'{0}+{1}'.format(prefix, key)
return key
key = unichr(event_key)
# qt delivers capitalized letters. fix capitalization
# note that capslock is ignored
if 'shift' in mods:
mods.remove('shift')
else:
key = key.lower()

mods.reverse()
return u'+'.join(mods + [key])

def new_timer(self, *args, **kwargs):
"""
Expand Down
0