Skip to content
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

Allow copy, paste etc. #150

Merged
merged 1 commit into from Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 notifications
Expand Down Expand Up @@ -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)

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