Qt implements Event Filtering in Mac Cocoa

Source: Internet
Author: User

Qt provides platform-related event filtering functions for different platforms, such

Bool QApplication: x11EventFilter (XEvent * event) [virtual]

Generally, a developer can derive a QApplication and then rewrite the function to obtain all X11 events of the program. Similar functions can be rewritten on other platforms. However, when I was working on Mac-related programs, I found such a paragraph in the document.

"Bool QApplication: macEventFilter (EventHandlerCallRef caller, EventRef event) [virtual]

Warning: This virtual function is only implemented under Mac OS X when against Carbon ."

This indicates that when Cocoa is used, the standard Qt method cannot intercept program events. The document also describes how to do this in Cocoa to get the event:

"Cocoa uses a different event system which means this function is not called when building Qt against Cocoa. if you want similar functionality subclass NSApplication and reimplement the sendEvent: message to handle all the NSEvents. you also will need to instantiate your custom NSApplication before creating a QApplication. seeApple's NSApplication Reference for more information."

This article is very detailed and specific, but since I know little about Mac programming, I still feel a little confused at the moment. I believe many of my friends have the same concerns as the author. By searching for examples and documents on the Internet, I have finally completed a small example. I would like to share it with many qters and hope to help you. Let's talk less about the Code:

 
  1. #include 
  2.  
  3. #include 
  4.  
  5. #include "mainwin.h" 
  6.  
  7. @interface KeyLoggerApplication : NSApplication 
  8.  
  9. { 
  10.  
  11. } 
  12.  
  13. @end 
  14.  
  15. @implementation KeyLoggerApplication 
  16.  
  17. - (BOOL)sendEvent:(NSEvent *)anEvent { 
  18.  
  19. NSEventType type = [anEvent type]; 
  20.  
  21. bool handled = NO; 
  22.  
  23. if (type == NSKeyUp) 
  24.  
  25. { 
  26.  
  27. switch( [anEvent keyCode] ) 
  28.  
  29. { 
  30.  
  31. default: 
  32.  
  33. NSLog(@"Keypressed: %d, **%@**", [anEvent keyCode], [anEvent characters]); 
  34.  
  35. break; 
  36.  
  37. } 
  38.  
  39. } 
  40.  
  41. //handle only the keys i need then let the other go through the regular channels 
  42.  
  43. //this stops the annoying beep 
  44.  
  45. if( !handled ) 
  46.  
  47. [super sendEvent:anEvent]; 
  48.  
  49. } 
  50.  
  51. @end 
  52.  
  53. int main(int argc, char* argv[]) 
  54.  
  55. { 
  56.  
  57. [KeyLoggerApplication sharedApplication]; 
  58.  
  59. QApplication a(argc, argv); 
  60.  
  61. MainWin mw; 
  62.  
  63. mw.show(); 
  64.  
  65. return a.exec(); 
  66.  
  67. } 

The above code prints the keyboard-pressed event to the console. In addition to the syntax of Objective C, there is no difficulty. I believe everyone can understand it. Another note is that the file stored in this code must be. mm is the suffix, that is, the main. cpp should be changed to main. mm, the corresponding pro file also needs to be modified. "LIBS + =-framework AppKit" must be added to the pro, because NSApplication and other APIs provided by AppKit are used.

I personally think these strange Mac programming rules are a little too different, and I don't like them very much.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.