Advertisement
arabasso

MyWindow.mm

Mar 31st, 2025
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Objective C 2.38 KB | Source Code | 0 0
  1. // MyWindow.mm
  2. #import <Cocoa/Cocoa.h>  // Importando o framework Cocoa
  3. #include "MyWindow.hpp"    // Incluindo o cabeçalho da classe
  4.  
  5. // Implementação da classe MyWindow
  6. MyWindow::MyWindow() {
  7.     // Construtor vazio, aqui você pode inicializar membros, se necessário
  8. }
  9.  
  10. MyWindow::~MyWindow() {
  11.     // Destruidor, pode limpar recursos se necessário
  12. }
  13.  
  14. void MyWindow::run() {
  15.     // Criando a janela usando Cocoa
  16.     @autoreleasepool {
  17.         // Criar uma instância da aplicação
  18.         NSApplication *app = [NSApplication sharedApplication];
  19.  
  20.         // Criar uma janela
  21.         NSWindow *window = [[NSWindow alloc]
  22.                             initWithContentRect:NSMakeRect(0, 0, 800, 600)
  23.                             styleMask:(NSWindowStyleMaskTitled |
  24.                                         NSWindowStyleMaskClosable |
  25.                                         NSWindowStyleMaskResizable)
  26.                             backing:NSBackingStoreBuffered
  27.                             defer:NO];
  28.         [window setTitle:@"Minha Janela"];
  29.         [window makeKeyAndOrderFront:nil];
  30.  
  31.         // Instalação do evento de monitoramento do mouse e teclado
  32.         [window setAcceptsMouseMovedEvents:YES]; // Aceita eventos de movimento de mouse
  33.  
  34.         // Lógica para tratar eventos (mouse e teclado)
  35.         while (![app isRunning]) {
  36.             NSEvent *event = [app nextEventMatchingMask:NSEventMaskAny untilDate:nil inMode:NSDefaultRunLoopMode dequeue:YES];
  37.             [app sendEvent:event];
  38.             [app updateWindows];
  39.  
  40.             // Checando tipo de evento (mouse ou teclado)
  41.             if ([event type] == NSEventTypeKeyDown) {
  42.                 NSString *keyPressed = [event charactersIgnoringModifiers];
  43.                 std::cout << "Tecla pressionada: " << [keyPressed UTF8String] << std::endl;
  44.             }
  45.  
  46.             if ([event type] == NSEventTypeMouseMoved) {
  47.                 NSPoint mouseLocation = [event locationInWindow];
  48.                 std::cout << "Mouse movido para: (" << mouseLocation.x << ", " << mouseLocation.y << ")" << std::endl;
  49.             }
  50.  
  51.             if ([event type] == NSEventTypeLeftMouseDown) {
  52.                 NSPoint mouseDownLocation = [event locationInWindow];
  53.                 std::cout << "Clique do mouse em: (" << mouseDownLocation.x << ", " << mouseDownLocation.y << ")" << std::endl;
  54.             }
  55.         }
  56.     }
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement