为了响应键盘事件,AX增加了一个系统方法task,在用户点击某些键时会调用Form的task方法,如果想对这些键实现自己的响应,可以重载task方法,在其中判断当前的_taskId为哪个值,再写自己的响应code.比如用户选中ALT时,如下代码所示:
public int task(int _taskId)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
int ret;
#Task
if( _taskId == #taskAlt)
info("I am 'Alt'");
return true;
}
由于Task方法的调用是由平台实现的,没找到它实现的代码,并不是所有的按键都会调用task方法,只有在宏Macro->Task中定义的键被点击时才会调用,有一些组合键比如Ctrl+Shift+Enter这些在该宏中没有.
那如果用户点击某些该宏不存在的键时想触发自己定义的处理代码该怎么办那?只能用WinAPI了.
1.在类WINAPI中添加如下静态方法供调用:
client static int getAsyncKeyState(int _key)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
DLL dll = new DLL('USER32');
DLLFunction getAsyncKeyState = new DLLFunction(dll,'GetAsyncKeyState');
![](/Images/OutliningIndicators/InBlock.gif)
getAsyncKeyState.returns(ExtTypes::WORD);
getAsyncKeyState.arg(ExtTypes::DWORD);
![](/Images/OutliningIndicators/InBlock.gif)
return getAsyncKeyState.call(_key);
}
2.添加方法调用步骤1中的方法检测用户的按键情况:
void catchKey()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
int ctrl,shift,enter;
#define.bingo(32768)
;
![](/Images/OutliningIndicators/InBlock.gif)
ctrl = WinAPI::getAsyncKeyState(0x11);
shift = WinAPI::getAsyncKeyState(0xA0);
enter = WinAPI::getAsyncKeyState(0x0D);
![](/Images/OutliningIndicators/InBlock.gif)
if ( ctrl == #bingo &&
shift == #bingo &&
enter == #bingo)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
info("Bingo");
}
![](/Images/OutliningIndicators/InBlock.gif)
this.setTimeOut("catchKey",10,false);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
注意上面的setTimeOut方法,定时周期性调用catchKey方法检测用户的按键情况.
3.重载run方法,调用catchKey方法.
void run()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
;
super();
element.catchKey();
}
关于WinAPI
'GetAsyncKeyState'的用法,可以参加如下链接:
http://msdn2.microsoft.com/en-us/library/ms646293.aspx
OK,就这些了.