Skip to content

Commit

Permalink
Fix KeySpec.from_string (#638)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwiggins committed Feb 26, 2021
1 parent c39183f commit 9600970
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion enable/base_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def from_string(cls, s):
codes = s.split("+")
key = codes[-1]
modifiers = set(code.lower() for code in codes[:-1])
ignore = set("alt", "shift", "control") - modifiers
ignore = {"alt", "shift", "control"} - modifiers
return cls(key, *modifiers, ignore=ignore)


Expand Down
41 changes: 41 additions & 0 deletions enable/tests/test_key_spec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# (C) Copyright 2005-2021 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
import collections
import unittest

from enable.api import KeySpec

Event = collections.namedtuple(
"Event", "character,alt_down,control_down,shift_down"
)


class TestKeySpec(unittest.TestCase):
def test_basics(self):
spec = KeySpec("Right", "control", ignore=["shift"])

self.assertEqual(spec.key, "Right")
self.assertTrue(spec.control)
self.assertSetEqual(spec.ignore, {"shift"})
self.assertFalse(spec.alt)
self.assertFalse(spec.shift)

event = Event("k", False, True, False)
self.assertFalse(spec.match(event))

event = Event("Right", False, True, False)
self.assertTrue(spec.match(event))

def test_from_string(self):
spec = KeySpec.from_string("Shift+Control+z")
self.assertSetEqual(spec.ignore, {"alt"})

event = Event("z", False, True, True)
self.assertTrue(spec.match(event))

0 comments on commit 9600970

Please sign in to comment.