响应键盘事件

为了响应键盘事件,AX增加了一个系统方法task,在用户点击某些键时会调用Form的task方法,如果想对这些键实现自己的响应,可以重载task方法,在其中判断当前的_taskId为哪个值,再写自己的响应code.比如用户选中ALT时,如下代码所示:
public int task(int _taskId)
{
    
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)
{
 
    DLL             dll                 
= new DLL('USER32');
    DLLFunction     getAsyncKeyState    
= new DLLFunction(dll,'GetAsyncKeyState');

    getAsyncKeyState.returns(ExtTypes::WORD);
    getAsyncKeyState.arg(ExtTypes::DWORD);

     
return getAsyncKeyState.call(_key);
}
2.添加方法调用步骤1中的方法检测用户的按键情况:
void catchKey()
{

    
int ctrl,shift,enter;
    
#define.bingo(32768)
    ;

    ctrl    
=   WinAPI::getAsyncKeyState(0x11);
    shift   
=   WinAPI::getAsyncKeyState(0xA0);
    enter   
=   WinAPI::getAsyncKeyState(0x0D);

    
if ( ctrl == #bingo &&
            shift 
== #bingo &&
                enter 
== #bingo)
    
{

        info(
"Bingo");
    }


    
this.setTimeOut("catchKey",10,false);

}

注意上面的setTimeOut方法,定时周期性调用catchKey方法检测用户的按键情况.
3.重载run方法,调用catchKey方法.
void run()
{
    ;
    super();
    element.catchKey();
}
关于WinAPI  'GetAsyncKeyState'的用法,可以参加如下链接:
http://msdn2.microsoft.com/en-us/library/ms646293.aspx
OK,就这些了.
原文地址:https://www.cnblogs.com/Farseer1215/p/1052146.html