C++【紀錄】ANE呼叫Windows原生鍵盤事件
需求:AS3直接對某個應用程序發送鍵盤事件
作法:AS3介接ANE 呼叫 WIN32 鍵盤事件API
至於如何打包ane,網上已經有一堆教學,可以參考以下文章
http://reader.roodo.com/sayaku/archives/21110640.html
上Code (AS3 Lib部分) ※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* copyData 傳送資料 | |
* @param appName | |
* @param opcocde | |
* @param itemID | |
*/ | |
public function sentToNativeApp(appName:String = "Win32Project1", opcocde:int = 0, itemID:int = 0):void | |
{ | |
//調用原生C++的SendWindowsApp方法 | |
_ec.call("SendWindowsApp", appName, opcocde, itemID); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FREObject SendWindowsApp(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[]) | |
{ | |
uint32_t len = 0; | |
const uint8_t* t = NULL; | |
const uint8_t* c = NULL; | |
uint32_t opcodes = 0; | |
//將AS3數據轉換成C++ | |
FREGetObjectAsUTF8(argv[0], &len, &t);//windows appName | |
FREGetObjectAsUint32(argv[1], &opcodes);//opcode | |
//AS3字串轉換 | |
char* text= (char*)t; | |
WCHAR windowsAppName[1024]; | |
swprintf_s(windowsAppName,L"%S",text); | |
//呼叫WindowsAPP | |
HWND hwDispatch = FindWindow( NULL, windowsAppName); | |
uint32_t KeyCode; | |
FREGetObjectAsUint32(argv[2], &KeyCode);//按鈕事件 | |
//如果WindowsAPP有在執行 | |
if( hwDispatch) | |
{ | |
ShowWindow(hwDispatch,SW_RESTORE); | |
BringWindowToTop(hwDispatch); | |
//強制抓焦點 | |
//SetFocus(hwDispatch); | |
//BringWindowToTop(hwDispatch); | |
SetForegroundWindow(hwDispatch); | |
} | |
keybd_event(KeyCode,0,0,0); | |
keybd_event(KeyCode,0,KEYEVENTF_KEYUP,0); | |
return argv[0]; | |
} | |
註記
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//打包SWC | |
acompc -source-path "你com資料夾所在目錄的完整路徑"-include-classes "NativeApp.as的路徑"-swf-version="你flashplayer的版本號"-output "輸出swc的路徑" | |
//打包ANE | |
adt -package -target ane NativeApp.ane "xml描述檔路徑" -swc "swc檔路徑"-platform Windows-x86 -C "原生dll檔與swf檔" |
張貼留言