Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Cocoa Key Event Question

Discussion in 'General Discussion' started by FelixAlias, Dec 20, 2005.

  1. FelixAlias

    FelixAlias

    Joined:
    Nov 6, 2005
    Posts:
    45
    Don't worry, this doesn't have to do with games :p (I will be getting a Unity license pretty soon). I'm wondering how to get a Cocoa (Fullscreen, if it should matter) app to capture keypresses so that when a user hits F8 the window appears (I have the code for checking what the keypress is, and handling it, all I need is watching it). Should I subclass something?

    Perhaps I should provide a bit more info:
    I'm working on a web browser that acts a bit like dashboard. I'm having trouble with the key commands.
    - In Controller.h and Controller.m I define the main window and the web panel
    - In MyWindow.m and h I subclass NSWindow so that it can become the key window (fullscreen).
    - If I put my code in MyWindow.m it doesn't work because mainwindow belongs to Controller and importing it won't work (I'm pretty sure I wouldn't instantiate it either).
    - If I put it in Controller it doesn't work because controller is, well; a controller.

    Any help would be appreciated!

    (PS I'm just learning Cocoa so I'm sure something I said will have not made sense or have been "the wrong way to do it")
     
  2. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    You might want to check out iDevApps and/or iDevGames for non-Unity related Mac programming help. :)

    I'm not a Cocoa programmer so I can't really help, unfortunately....
     
  3. FelixAlias

    FelixAlias

    Joined:
    Nov 6, 2005
    Posts:
    45
    I checked those, however I just found a solution (Google; actually, it is on iDevGames!)..

    - Subclass NSApplication.
    This works. You just subclass NSApplication and override sendEvent. If anybody would like a more specific way to do this, feel free to ask :D
    I got this information from the bottom of here:
    http://www.idevgames.com/forum/archive/index.php/t-6559.html
     
  4. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    Hmm.. I think thats a fix to using cmd + any key. But I think you can use ordinary key's without subclassing NSApplication... I might be wrong though (im learning too)

    Code (csharp):
    1.  
    2. - (void) keyDown:(NSEvent *)theEvent
    3. {
    4.    unichar unicodeKey;
    5.    unicodeKey = [ [ theEvent characters ] characterAtIndex:0 ];
    6.    switch( unicodeKey )
    7.    {
    8.             case 'f':
    9.                 //do something!
    10.             break;
    11.  
    12.             case NSUpArrowFunctionKey:
    13.                 //move something      
    14.             break;
    15.    }
    16. }
    17.  
     
  5. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    no wait a minute, it's Best just to subclass NSApp, my bad!