Skip to content

Commit

Permalink
Merge pull request #150 from cbenhagen/paste
Browse files Browse the repository at this point in the history
Allow copy, paste etc.
  • Loading branch information
daredoes committed Apr 8, 2022
2 parents e2bacc8 + 24668c5 commit 54ca623
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
7 changes: 4 additions & 3 deletions rumps/rumps.py
Expand Up @@ -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 events
Expand Down Expand Up @@ -777,9 +778,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)

Expand Down
32 changes: 32 additions & 0 deletions 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

0 comments on commit 54ca623

Please sign in to comment.