Qt在Mac Cocoa下实现事件过滤的方法

移动开发
本文将讲述Qt在Mac Cocoa下实现事件过滤的方法。

Qt为不同平台提供了平台相关的事件过滤函数, 如X11下为

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

一般情况下,开发者可以通过派生QApplication,然后重写该函数获得程序得到的所有X11事件。 在其他平台上也有类似的函数可以重写。 但笔者在做Mac相关的程序时在文档中发现了这样一段话

“bool QApplication::macEventFilter ( EventHandlerCallRef caller, EventRef event ) [virtual]

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

 

这说明在用Cocoa时, 标准的Qt方法没有办法截获程序的事件。 文档后面还描述了在Cocoa下该如何做才能得到事件:

 “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 to instantiate your custom NSApplication before creating a QApplication. SeeApple’s NSApplication Reference for more information.”

 

这段话说来算是很详细具体了, 但由于笔者对Mac编程所知甚少, 一时之间还是觉得有些无所适从, 相信很多朋友跟笔者有同样的疑虑。 通过在网上查找例子和文档, 笔者终于搞定了一个小例子, 特在此和广大qter分享, 希望对大家有所帮助。 闲话少说, 上代码:

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

上面这段代码将接收到的键盘按下的事件打印到console上。除了语法是奇怪的Objective C的语法之外, 没有什么难点, 相信大家都是看得懂的。 还有一点值得提醒的地方, 就是这段代码保存的文件必须以.mm为后缀名, 也就是我们通常写的main.cpp要改成main.mm, 相应的pro文件也要修改。 pro里还要加上 “LIBS+= -framework AppKit”,因为用到了AppKit提供的NSApplication等API。

个人觉得Mac编程的这些奇怪的条条框框有点太另类,俺是非常不喜欢的。

责任编辑:佚名 来源: cuteqt
相关推荐

2011-06-29 16:14:59

Qt 事件 过滤器

2011-07-04 14:38:43

QT Qevent

2011-07-28 16:52:34

Cocoa 框架 Mac Os

2011-08-02 13:58:18

Cocoa 框架 Mac OS

2011-06-17 11:00:18

Qt Linux Ubuntu

2011-07-06 15:06:46

Xcode Cocoa

2011-07-20 09:49:41

Xcode Interface Builder

2011-06-29 11:22:06

Qt Windows 入口函数

2021-06-04 09:01:27

Cocoa 协议编程 Swift

2011-06-13 17:46:07

Qt 串口通信

2011-07-07 10:29:35

Cocoa 方法 框架

2011-08-09 13:34:53

SubversionXCodeMac

2011-06-30 09:46:01

QT 显示视频 linux

2011-06-21 17:01:44

Qt 静态 编译

2011-03-23 15:58:50

全局热键QtWindows

2022-07-20 23:04:59

矩阵分解算法Spark

2011-03-03 13:46:00

NTFS-3G

2011-06-16 10:09:25

QT Windows DLL

2011-07-02 13:24:39

QT Linux

2011-08-11 15:46:55

CocoaCocoa Touch框架
点赞
收藏

51CTO技术栈公众号