自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Qt在Mac Cocoa下實現(xiàn)事件過濾的方法

移動開發(fā)
本文將講述Qt在Mac Cocoa下實現(xiàn)事件過濾的方法。

Qt為不同平臺提供了平臺相關(guān)的事件過濾函數(shù), 如X11下為

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

一般情況下,開發(fā)者可以通過派生QApplication,然后重寫該函數(shù)獲得程序得到的所有X11事件。 在其他平臺上也有類似的函數(shù)可以重寫。 但筆者在做Mac相關(guān)的程序時在文檔中發(fā)現(xiàn)了這樣一段話

“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編程所知甚少, 一時之間還是覺得有些無所適從, 相信很多朋友跟筆者有同樣的疑慮。 通過在網(wǎng)上查找例子和文檔, 筆者終于搞定了一個小例子, 特在此和廣大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, 相應(yīng)的pro文件也要修改。 pro里還要加上 “LIBS+= -framework AppKit”,因為用到了AppKit提供的NSApplication等API。

個人覺得Mac編程的這些奇怪的條條框框有點太另類,俺是非常不喜歡的。

責任編輯:佚名 來源: cuteqt
相關(guān)推薦

2011-06-29 16:14:59

Qt 事件 過濾器

2011-07-04 14:38:43

QT Qevent

2011-08-02 13:58:18

Cocoa 框架 Mac OS

2011-07-28 16:52:34

Cocoa 框架 Mac Os

2011-06-17 11:00:18

Qt Linux Ubuntu

2011-07-20 09:49:41

Xcode Interface Builder

2011-07-06 15:06:46

Xcode Cocoa

2011-06-29 11:22:06

Qt Windows 入口函數(shù)

2021-06-04 09:01:27

Cocoa 協(xié)議編程 Swift

2011-07-07 10:29:35

Cocoa 方法 框架

2011-06-30 09:46:01

QT 顯示視頻 linux

2011-06-13 17:46:07

Qt 串口通信

2011-08-09 13:34:53

SubversionXCodeMac

2011-06-16 10:09:25

QT Windows DLL

2011-03-23 15:58:50

全局熱鍵QtWindows

2011-06-21 17:01:44

Qt 靜態(tài) 編譯

2011-07-02 13:24:39

QT Linux

2011-08-11 15:46:55

CocoaCocoa Touch框架

2011-03-03 13:46:00

NTFS-3G

2022-07-20 23:04:59

矩陣分解算法Spark
點贊
收藏

51CTO技術(shù)棧公眾號