From 24668c513248efda8bb44e564c850e2faf1cf3a2 Mon Sep 17 00:00:00 2001 From: Ben Hagen Date: Tue, 29 Dec 2020 18:25:38 +0100 Subject: [PATCH] Allow copy, paste etc. --- rumps/rumps.py | 7 ++++--- rumps/text_field.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 rumps/text_field.py diff --git a/rumps/rumps.py b/rumps/rumps.py index d8f8404..7543490 100644 --- a/rumps/rumps.py +++ b/rumps/rumps.py @@ -20,8 +20,9 @@ import traceback import weakref -from .utils import ListDict from .compat import text_type, string_types, iteritems, collections_abc +from .text_field import Editing, SecureEditing +from .utils import ListDict from . import _internal from . import notifications @@ -746,9 +747,9 @@ def __init__(self, message='', title='', default_text='', ok=None, cancel=None, self._alert.setAlertStyle_(0) # informational style if secure: - self._textfield = NSSecureTextField.alloc().initWithFrame_(NSMakeRect(0, 0, *dimensions)) + self._textfield = SecureEditing.alloc().initWithFrame_(NSMakeRect(0, 0, *dimensions)) else: - self._textfield = NSTextField.alloc().initWithFrame_(NSMakeRect(0, 0, *dimensions)) + self._textfield = Editing.alloc().initWithFrame_(NSMakeRect(0, 0, *dimensions)) self._textfield.setSelectable_(True) self._alert.setAccessoryView_(self._textfield) diff --git a/rumps/text_field.py b/rumps/text_field.py new file mode 100644 index 0000000..f33f0ff --- /dev/null +++ b/rumps/text_field.py @@ -0,0 +1,32 @@ +from AppKit import NSApplication, NSTextField, NSSecureTextField, NSKeyDown, NSCommandKeyMask + + +class Editing(NSTextField): + """NSTextField with cut, copy, paste, undo and selectAll""" + def performKeyEquivalent_(self, event): + return _perform_key_equivalent(self, event) + + +class SecureEditing(NSSecureTextField): + """NSSecureTextField with cut, copy, paste, undo and selectAll""" + def performKeyEquivalent_(self, event): + return _perform_key_equivalent(self, event) + + +def _perform_key_equivalent(self, event): + if event.type() == NSKeyDown and event.modifierFlags() & NSCommandKeyMask: + if event.charactersIgnoringModifiers() == "x": + NSApplication.sharedApplication().sendAction_to_from_("cut:", None, self) + return True + elif event.charactersIgnoringModifiers() == "c": + NSApplication.sharedApplication().sendAction_to_from_("copy:", None, self) + return True + elif event.charactersIgnoringModifiers() == "v": + NSApplication.sharedApplication().sendAction_to_from_("paste:", None, self) + return True + elif event.charactersIgnoringModifiers() == "z": + NSApplication.sharedApplication().sendAction_to_from_("undo:", None, self) + return True + elif event.charactersIgnoringModifiers() == "a": + NSApplication.sharedApplication().sendAction_to_from_("selectAll:", None, self) + return True