Fix modifier handling

We don't want to show <Shift-Shift>, but <Ctrl-Shift> should still work
correctly.
This commit is contained in:
Florian Bruhin 2018-03-01 23:04:00 +01:00
parent 7cb781cc92
commit 3a11a24be0
2 changed files with 23 additions and 25 deletions

View File

@ -33,14 +33,6 @@ def is_printable(key):
return key <= 0xff
def is_modifier_key(key):
# FIXME docs
return key in (Qt.Key_Control, Qt.Key_Alt, Qt.Key_Shift, Qt.Key_Meta,
Qt.Key_AltGr, Qt.Key_Super_L, Qt.Key_Super_R,
Qt.Key_Hyper_L, Qt.Key_Hyper_R, Qt.Key_Direction_L,
Qt.Key_Direction_R)
def _key_to_string(key):
"""Convert a Qt::Key member to a meaningful name.
@ -203,10 +195,18 @@ class KeyInfo:
A name of the key (combination) as a string.
"""
key_string = _key_to_string(self.key)
modifier_map = {
Qt.Key_Shift: Qt.ShiftModifier,
Qt.Key_Control: Qt.ControlModifier,
Qt.Key_Alt: Qt.AltModifier,
Qt.Key_Meta: Qt.MetaModifier,
Qt.Key_Mode_switch: Qt.GroupSwitchModifier,
}
modifiers = int(self.modifiers)
if is_modifier_key(self.key):
if self.key in modifier_map:
# Don't return e.g. <Shift+Shift>
return '<{}>'.format(key_string)
modifiers &= ~modifier_map[self.key]
elif is_printable(self.key):
# "normal" binding
# FIXME Add a test to make sure Tab doesn't become TAB
@ -220,7 +220,7 @@ class KeyInfo:
key_string = key_string.lower()
# "special" binding
modifier_string = QKeySequence(self.modifiers).toString()
modifier_string = QKeySequence(modifiers).toString()
return '<{}{}>'.format(modifier_string, key_string)
def text(self):

View File

@ -115,12 +115,6 @@ class TestKeyEventToString:
modifiers=Qt.ControlModifier)
assert str(keyutils.KeyInfo.from_event(evt)) == '<Control>'
def test_only_hyper_l(self, fake_keyevent_factory):
"""Test keyeevent when only Hyper_L is pressed."""
evt = fake_keyevent_factory(key=Qt.Key_Hyper_L,
modifiers=Qt.MetaModifier)
assert str(keyutils.KeyInfo.from_event(evt)) == '<Hyper L>'
def test_only_key(self, fake_keyevent_factory):
"""Test with a simple key pressed."""
evt = fake_keyevent_factory(key=Qt.Key_A)
@ -140,6 +134,18 @@ class TestKeyEventToString:
s = str(keyutils.KeyInfo.from_event(evt))
assert s == '<Meta+Ctrl+Alt+Shift+a>'
def test_modifier_key(self, fake_keyevent_factory):
evt = fake_keyevent_factory(key=Qt.Key_Shift,
modifiers=Qt.ShiftModifier)
s = str(keyutils.KeyInfo.from_event(evt))
assert s == '<Shift>'
def test_modifier_key_with_modifiers(self, fake_keyevent_factory):
evt = fake_keyevent_factory(key=Qt.Key_Shift,
modifiers=(Qt.ShiftModifier |
Qt.ControlModifier))
s = str(keyutils.KeyInfo.from_event(evt))
assert s == '<Ctrl+Shift>'
@pytest.mark.parametrize('keystr, expected', [
('<Control-x>', keyutils.KeySequence(Qt.ControlModifier | Qt.Key_X)),
@ -184,11 +190,3 @@ def test_normalize_keystr(orig, normalized):
])
def test_is_printable(key, printable):
assert keyutils.is_printable(key) == printable
@pytest.mark.parametrize('key, ismodifier', [
(Qt.Key_Control, True),
(Qt.Key_X, False)
])
def test_is_modifier_key(key, ismodifier):
assert keyutils.is_modifier_key(key) == ismodifier