MyApplication.mm 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /******************************************************************************/
  2. #include "stdafx.h"
  3. #include "MyApplication.h"
  4. namespace EE{
  5. /******************************************************************************/
  6. struct KeyState
  7. {
  8. KB_KEY b ;
  9. Bool on;
  10. };
  11. /******************************************************************************/
  12. extern VecI2 MouseIgnore;
  13. static Bool Active; void SetActive() {App.setActive(Active && !App.minimized());}
  14. /******************************************************************************/
  15. }
  16. /******************************************************************************/
  17. // APP DELEGATE
  18. /******************************************************************************/
  19. @implementation MyAppDelegate
  20. -(void)applicationDidBecomeActive:(NSNotification*)notification {Active=true ; SetActive();}
  21. -(void)applicationDidResignActive:(NSNotification*)notification {Active=false; SetActive();}
  22. @end
  23. /******************************************************************************/
  24. // APP
  25. /******************************************************************************/
  26. @implementation MyApplication
  27. - (void)dealloc
  28. {
  29. [self.delegate release]; [self setDelegate:nil];
  30. [super dealloc];
  31. }
  32. -(void)sendEvent:(NSEvent*)theEvent // key input must be processed here to: capture KB_INS (insert/help key) as it is eaten by default NSWindow class and not processed further, and to capture "key up" message when using ctrl/cmd+key (as it is normally eaten by menu/toolbar)
  33. {
  34. switch([theEvent type])
  35. {
  36. case NSKeyDown:
  37. {
  38. UInt code=[theEvent keyCode];
  39. if(InRange(code, ScanCodeToKey))
  40. {
  41. KB_KEY key=ScanCodeToKey[code];
  42. Kb.push(key, code);
  43. // !! queue characters after push !!
  44. if(Kb.anyCtrl()) // if Control is pressed then no characters will be received, we must add them basing on 'key'
  45. {
  46. if(key>='A' && key<='Z')Kb.queue(Char(key + (Kb.anyShift() ? 0 : 'a'-'A')), code);else
  47. if(key>='0' && key<='9')Kb.queue(Char(key ), code);
  48. }
  49. }
  50. // !! queue characters after push !!
  51. if(NSString *characters=[theEvent characters])FREP([characters length])
  52. {
  53. UInt c=[characters characterAtIndex:i];
  54. if( c>=32 && c<0xF700 && c!=127 || c>0xF8FF) // range reserved by Apple for functional keys (not character keys) 127=DEL
  55. {
  56. if(Kb.anyWin() && Kb.anyShift())c=CaseUp(Char(c)); // if Win+Shift are pressed, then pressing characters will not generate them upper case, so do this manually
  57. Kb.queue(Char(c), code);
  58. }
  59. }
  60. }return;
  61. case NSKeyUp:
  62. {
  63. UInt code=[theEvent keyCode];
  64. if(InRange(code, ScanCodeToKey))Kb.release(ScanCodeToKey[code]);
  65. }return;
  66. case NSFlagsChanged:
  67. {
  68. UInt f=[theEvent modifierFlags];
  69. KeyState ks[]=
  70. {
  71. {KB_LCTRL , FlagTest(f, Kb.swappedCtrlCmd() ? 0x00008 : 0x00001)},
  72. {KB_RCTRL , FlagTest(f, Kb.swappedCtrlCmd() ? 0x00010 : 0x02000)},
  73. {KB_LSHIFT, FlagTest(f, 0x00002)},
  74. {KB_RSHIFT, FlagTest(f, 0x00004)},
  75. {KB_LALT , FlagTest(f, 0x00020)},
  76. {KB_RALT , FlagTest(f, 0x00040)},
  77. {KB_LWIN , FlagTest(f, Kb.swappedCtrlCmd() ? 0x00001 : 0x00008)},
  78. {KB_RWIN , FlagTest(f, Kb.swappedCtrlCmd() ? 0x02000 : 0x00010)},
  79. {KB_CAPS , FlagTest(f, 0x10000)}, // enables access to only whether CapsLock is on or off (not if pushed/released)
  80. };
  81. REPA(ks)if(Kb.b(ks[i].b)!=ks[i].on){if(ks[i].on)Kb.push(ks[i].b, -1);else Kb.release(ks[i].b);}
  82. }return;
  83. /*case NSAppKitDefined:
  84. {
  85. switch([theEvent subtype])
  86. {
  87. case NSApplicationActivatedEventType:
  88. {
  89. }break;
  90. case NSApplicationDeactivatedEventType:
  91. {
  92. }break;
  93. }
  94. }break;*/
  95. case NSMouseMoved:
  96. case NSLeftMouseDragged:
  97. case NSRightMouseDragged:
  98. case NSOtherMouseDragged:
  99. {
  100. Ms._delta_relative.x+=[theEvent deltaX]-MouseIgnore.x; MouseIgnore.x=0;
  101. Ms._delta_relative.y-=[theEvent deltaY]-MouseIgnore.y; MouseIgnore.y=0;
  102. }return;
  103. }
  104. [super sendEvent:theEvent];
  105. }
  106. @end
  107. /******************************************************************************/