Guest User

Untitled

a guest
Nov 19th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. // *DISCLAIMER IF YOU FOUND THIS VIA GOOGLE: I'm at the time of writing still figuring this out, it
  2. // works for me but I don't actually know if it's correct yet*
  3. //
  4. // This is a minimal example of how one might accept text entry in Cocoa in an OpenGL app or
  5. // such. Unlike using `[event characters]`, it ignores unprintable keys such as the arrow keys. It
  6. // also allows you to type accents and such, e.g. `alt+e o`. This example contains no error
  7. // checking, and the only way to exit it is to kill it from the command line.
  8. //
  9. // To execute it, run:
  10. // `$ gcc minimal_cocoa_gl_text_entry.m -framework Cocoa && ./a.out`
  11.  
  12. #import <Cocoa/Cocoa.h>
  13.  
  14. // We need to create a new subclass of `NsView` that implements the `NSTextInputClient` protocol.
  15. // NSView can be replaced with NSOpenGLView or such here if needed.
  16. //
  17. // It's possible that instead you could just have a separate `NSTextInputClient` object, and send
  18. // the `keyDown` events to it from here instead. I haven't yet tried this.
  19. @interface MyView : NSView<NSTextInputClient>
  20. {
  21. }
  22. @end
  23.  
  24. @implementation MyView
  25. // Accept first responder so we get events
  26. - (BOOL)acceptsFirstResponder {
  27. return YES;
  28. }
  29.  
  30. // Interpret the key events as text
  31. - (void)keyDown:(NSEvent *)event {
  32. [self interpretKeyEvents:[NSArray arrayWithObject:event]];
  33. }
  34.  
  35. // Just log the text as we get it to show that it works
  36. - (void)insertText:(id)string replacementRange:(NSRange)replacementRange {
  37. NSLog(@"%@", string);
  38. }
  39.  
  40. // The rest of these are methods I don't yet understand and have just filled in stubs for so it
  41. // would compile.
  42. - (BOOL)hasMarkedText {
  43. return NO;
  44. }
  45.  
  46. - (NSRange)markedRange {
  47. return NSMakeRange(0, 0);
  48. }
  49.  
  50. - (NSRange)selectedRange {
  51. return NSMakeRange(0, 0);
  52. }
  53.  
  54. - (void)setMarkedText:(id)string
  55. selectedRange:(NSRange)selectedRange
  56. replacementRange:(NSRange)replacementRange {
  57. }
  58.  
  59. - (void)unmarkText {
  60. }
  61.  
  62. - (NSArray*)validAttributesForMarkedText {
  63. return [NSArray array];
  64. }
  65.  
  66. - (NSAttributedString*)attributedSubstringForProposedRange:(NSRange)range
  67. actualRange:(NSRangePointer)actualRange {
  68. return nil;
  69. }
  70.  
  71. - (NSUInteger)characterIndexForPoint:(NSPoint)point {
  72. return 0;
  73. }
  74.  
  75. - (NSRect)firstRectForCharacterRange:(NSRange)range
  76. actualRange:(NSRangePointer)actualRange {
  77. return NSMakeRect(0, 0, 0, 0);
  78. }
  79.  
  80. - (void)doCommandBySelector:(SEL)selector {
  81. }
  82. @end
  83.  
  84. int main() {
  85. @autoreleasepool {
  86. NSApplication* app = [NSApplication sharedApplication];
  87. [app setActivationPolicy: NSApplicationActivationPolicyRegular];
  88.  
  89. NSWindow *window = [[[NSWindow alloc]
  90. initWithContentRect: NSMakeRect(0, 0, 200, 200)
  91. styleMask: NSWindowStyleMaskTitled
  92. backing: NSBackingStoreBuffered
  93. defer: NO
  94. ] autorelease];
  95.  
  96. [window setContentView: [[MyView alloc] init]]; // Here we're using our custom view
  97.  
  98. [window cascadeTopLeftFromPoint:NSMakePoint(20,20)];
  99. [window makeKeyAndOrderFront:nil];
  100. [NSApp activateIgnoringOtherApps:YES];
  101. [app run];
  102. }
  103. return 0;
  104. }
Add Comment
Please, Sign In to add comment