15#include <wx/textctrl.h>
16#include <AppKit/AppKit.h>
17#include <wx/osx/core/private.h>
18#include <wx/osx/cocoa/private.h>
19#elif defined(__WXGTK__)
25#include <wx/eventfilter.h>
26#include <wx/toplevel.h>
27#include <wx/weakref.h>
41 static wxWindowRef theHandler;
72void OnFocus( wxWindow &window, wxFocusEvent &event )
74 if (event.GetEventType() == wxEVT_KILL_FOCUS)
79 window.Refresh(
false );
100 NSEventMask mask = NSKeyDownMask | NSKeyUpMask;
104 NSEvent addLocalMonitorForEventsMatchingMask:mask
handler:^NSEvent *(NSEvent *event)
106 WXWidget widget = (WXWidget) [ [event window] firstResponder];
109 wxWidgetCocoaImpl *impl = (wxWidgetCocoaImpl *)
110 wxWidgetImpl::FindFromWXWidget(widget);
115 wxKeyEvent wxevent([event type] == NSKeyDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
116 impl->SetupKeyEvent(wxevent, event);
119 if ([event type] == NSKeyDown)
121 wxKeyEvent eventHook(wxEVT_CHAR_HOOK, wxevent);
122 result =
FilterEvent(eventHook) == Event_Processed ? nil : event;
126 result =
FilterEvent(wxevent) == Event_Processed ? nil : event;
140 wxEvtHandler::AddFilter(
this);
143 wxEvtHandler::AddFilter(
this);
150#if defined(__WXMAC__)
151 wxEvtHandler::RemoveFilter(
this);
154 wxEvtHandler::RemoveFilter(
this);
162 return GuardedCall< int > ( [&] {
164 wxEventType type =
event.GetEventType();
165 if (type != wxEVT_CHAR_HOOK && type != wxEVT_KEY_UP)
170 wxKeyEvent
key =
static_cast<wxKeyEvent &
>( event );
180 if ( type == wxEVT_CHAR_HOOK &&
181 key.GetKeyCode() == WXK_RETURN ) {
185 const auto text =
dynamic_cast<wxTextCtrl*
>(focus);
186 if ( !(text && text->IsMultiLine()) ) {
188 dynamic_cast< wxTopLevelWindow*
>(
189 wxGetTopLevelParent( focus ) ) ) {
191 dynamic_cast<wxButton*
>( top->GetDefaultItem() ) ) {
192 wxCommandEvent newEvent{ wxEVT_BUTTON, button->GetId() };
193 newEvent.SetEventObject( button );
194 button->GetEventHandler()->AddPendingEvent( newEvent );
195 return Event_Processed;
204 if (type == wxEVT_CHAR_HOOK)
206 key.SetEventType(wxEVT_KEY_DOWN);
213 return Event_Processed;
217 return Event_Processed;
226 wxTheApp->OnExceptionInMainLoop();
229 return Event_Processed;
245 wxEvtHandler *
handler = target->GetEventHandler();
248 wxKeyEvent temp = event;
250#if defined(__WXGTK__)
253 if (temp.GetModifiers() == (wxMOD_CONTROL | wxMOD_ALT))
255 temp.SetControlDown(
false);
256 temp.SetAltDown(
false);
262 wxCommandEvent e(EVT_CAPTURE_KEY);
263 e.SetEventObject(&temp);
271 bool keyDown = temp.GetEventType() == wxEVT_KEY_DOWN;
273 temp.StopPropagation();
274 wxEventProcessInHandlerOnly onlyDown(temp,
handler);
275 bool processed =
handler->ProcessEvent(temp);
279 if (!processed && keyDown)
291 for (
size_t i = 0, cnt = chars.length(); i < cnt; i++)
294 temp.SetEventType(wxEVT_CHAR);
296 temp.StopPropagation();
297 temp.m_uniChar = chars[i];
298 wxEventProcessInHandlerOnly onlyChar(temp,
handler);
313#if defined(__WXMSW__)
316 GetKeyboardState(ks);
318 int res = ToUnicode(event.GetRawKeyCode(), 0, ks, ucode, 256, 0);
321 chars.Append(ucode, res);
324#elif defined(__WXGTK__)
326 chars.Append((wxChar) gdk_keyval_to_unicode(event.GetRawKeyCode()));
328#elif defined(__WXMAC__)
337 return event.GetUnicodeKey();
340 NSString *c = [
mEvent charactersIgnoringModifiers];
343 unichar codepoint = [c characterAtIndex:0];
344 if ((codepoint >= 0xF700 && codepoint <= 0xF8FF) || codepoint == 0x7F)
351 chars = [c UTF8String];
353 auto uchr =
static_cast<CFDataRef
>(TISGetInputSourceProperty(
354 CF_ptr<TISInputSourceRef>{ TISCopyCurrentKeyboardInputSource() }
356 kTISPropertyUnicodeKeyLayoutData));
360 const UCKeyboardLayout *keyboardLayout = (
const UCKeyboardLayout*)CFDataGetBytePtr(uchr);
361 if (keyboardLayout == NULL)
366 const UniCharCount maxStringLength = 255;
367 UniCharCount actualStringLength = 0;
368 UniChar unicodeString[maxStringLength];
369 UInt32 nsflags = [
mEvent modifierFlags];
370 UInt16 modifiers = (nsflags & NSAlphaShiftKeyMask ? alphaLock : 0) |
371 (nsflags & NSShiftKeyMask ? shiftKey : 0) |
372 (nsflags & NSControlKeyMask ? controlKey : 0) |
373 (nsflags & NSAlternateKeyMask ? optionKey : 0) |
374 (nsflags & NSCommandKeyMask ? cmdKey : 0);
376 OSStatus status = UCKeyTranslate(keyboardLayout,
379 (modifiers >> 8) & 0xff,
392 chars = [ [NSString stringWithCharacters:unicodeString
393 length:(NSInteger)actualStringLength] UTF8String];
402#if defined(__WXMAC__)
Declare abstract class AudacityException, some often-used subclasses, and GuardedCall.
Wrap resource pointers from Apple Core SDK for RAII.
DEFINE_EVENT_TYPE(EVT_CAPTURE_KEY)
Custom events.
Base class for exceptions specially processed by the application.
wxString GetUnicodeString(const wxKeyEvent &event)
int FilterEvent(wxEvent &event) override
bool HandleCapture(wxWindow *target, const wxKeyEvent &event)
static result_type Call(Arguments &&...arguments)
Null check of the installed function is done for you.
std::unique_ptr< WindowPlacement > FindFocus()
Find the window that is accepting keyboard input, if any.
bool IsHandler(const wxWindow *handler)
void Release(wxWindow *handler)
void OnFocus(wxWindow &window, wxFocusEvent &event)
a function useful to implement a focus event handler The window releases the keyboard if the event is...
void Capture(wxWindow *handler)
AuthorizationHandler handler