utiliza tecla tab suprimir sirve que pro para macbook función forzar escape esc cuál bar apagado cocoa keyboard-shortcuts

cocoa - tab - tecla shift



¿Cómo puedo manejar la tecla ESC en la aplicación Cocoa? (4)

Hice una aplicación cambiando al modo de pantalla completa. Quiero usar la tecla ESC para escapar del modo de pantalla completa, pero el elemento de menú de enlace con la tecla ESC en IB se elimina en tiempo de ejecución. ¿Cómo puedo mantener el enlace de la tecla ESC a un elemento del menú?


La forma preferida de manejar la clave de escape en Cocoa es esta como dijo @Josh Caswell .

#pragma mark - NSResponder - (void)cancelOperation:(id)sender { [self exitFullScreen]; }


Muchas personas intentan implementar la funcionalidad de la tecla esc. Hay cancelOperation en la cadena de respuesta para manejar eventos de escape.

//WRONG - (void)keyDown:(NSEvent *)event { //unichar character = 0; //if ([event type] == NSEventTypeKeyDown) { // if ([[event charactersIgnoringModifiers] length] == 1) { // character = [[event characters] characterAtIndex:0]; // } //} switch (character) { //THIS IS WRONG correct is to implement interpretKeyEvents+moveRight //case NSRightArrowFunctionKey: // [self moveSelectedIndexRight]; // break; //THIS IS WRONG correct is to implement interpretKeyEvents+ moveLeft //case NSLeftArrowFunctionKey: // [self moveSelectedIndexLeft]; // break; //THIS IS WRONG correct is to implement interpretKeyEvents+ moveLeft //case NSCarriageReturnCharacter: // [self dismissWithCurrentlySelectedToken]; // break; default: [self interpretKeyEvents:@[event]]; [super keyDown:event] break; } } //CORRECT - (void)keyDown:(NSEvent *)event { [self interpretKeyEvents:@[event]]; [super keyDown:event]; } `/* Catch the commands interpreted by interpretKeyEvents:. Normally, if we don''t implement (or any other view in the hierarchy implements) the selector, the system beeps. Menu navigation generally doesn''t beep, so stop doCommandBySelector: from calling up t`he hierarchy just to stop the beep. */ - (void)doCommandBySelector:(SEL)selector { if ( selector == @selector(moveRight:) || selector == @selector(moveLeft:) || selector == @selector(cancelOperation:) || selector == @selector(insertNewline:) ) { [super doCommandBySelector:selector]; } // do nothing, let the menu handle it (see call to super in -keyDown:) // But don''t call super to prevent the system beep } - (void)cancelOperation:(id)sender { //do your escape stuff } - (void)insertNewline:(id)sender { //do your enter stuff } - (void)moveRight:(nullable id)sender { [self moveSelectedIndexRight]; } - (void)moveLeft:(nullable id)sender { [self moveSelectedIndexLeft]; }


Necesitaba esquivar los bloqueos de WKWebView cuando se presiona la tecla ESC (?), Así que la subclasifico y agregué:

import Carbon.HIToolbox override func keyDown(with event: NSEvent) { if event.keyCode == UInt16(kVK_Escape) { // We crash otherwise, so just close window self.window?.performClose(event) } else { // still here? super.keyDown(with: event) } }


Una forma de capturar eventos de teclado involucra subclases:

  1. Subclase su clase de pantalla completa (por ejemplo) NSView.
  2. Agregue el método - (void) keyDown:(NSEvent *)theEvent a la implementación de la subclase.
  3. Abra InterfaceBuilder y seleccione la clase de pantalla completa que creó anteriormente.
  4. Cambia su clase a tu nueva subclase.

La subclase se ve algo así como:

MySubclass.h

@interface MySubclass : NSView { } @end

MySubclass.m

#import <Carbon/Carbon.h> @implementation MySubclass - (void)keyDown:(NSEvent *)theEvent { switch([theEvent keyCode]) { case kVK_Escape: NSLog(@"ESC"); // Call the full-screen mode method break; default: [super keyDown:theEvent]; } } @end

Esto no vincula la tecla ESC con el elemento del menú, pero le brinda una funcionalidad equivalente (y un poco más de flexibilidad, ya que puede interceptar todos los eventos del teclado).