Advanced IntraWeb Async Event Handling
Posted by Olaf Monien in Delphi, Development, IntraWeb, tags: ajax, Delphi, IntraWeb, soundImplementing Asynchronous events with IntraWeb is plain and easy. Just hook some Pascal (or C++) code to any of the OnAsync Event handlers and you are done. The Advanced Ajax Events demo shows how to call Async events from your custom JavaScript routines and how to implement an asynchronous Key event handler.
This is all you need to Ajax-enable your IntraWeb application:
procedure TIWForm2.IWButton1AsyncClick(Sender: TObject; EventParams: TStringList);
begin
LabelResponse.Caption := 'Hello Ajax World!'
end;
Very simple, but what if you want to call async events from your own custom JavaScript? How to connect key events to some async IntraWeb handler?
This is the Delphi side of an async event handler, that listens to CTRL-A in the Web browser:
procedure TIWForm2.IWAppFormCreate(Sender: TObject);
begin
WebApplication.RegisterCallBack('OnCtrlA', OnCtrlA);
end;
procedure TIWForm2.OnCtrlA(EventParams: TStringList);
begin
WebApplication.ShowMessage('You pressed [CTRL - A] - Edit value: '+ IWEdit1.Text);
end;
The code in the Form’s JavaScript property implements the JavaScript side and shows how to call a “CallBack” (aka Delphi Async event handler) manually:
function KeyDownHandler(event){
//Hide for CTRL-A key from browser
if (event.keyCode==65 && event.ctrlKey)
{
event.returnValue = false;
return false; }
else {
return true;
}};
function KeyUpHandler(event){
//check for CTRL-A key
if (event.keyCode==65 && event.ctrlKey) {
processAjaxEvent(event, null,"OnCtrlA",false, null, true);
//hide from further "external" handling
event.returnValue = false;
return false;
} else {
return true;
} };
//connect event handlers
window.onkeyup = KeyUpHandler;
window.onkeydown = KeyDownHandler;
Below is a more complex demo project. Please note that there are differences between browsers (IE, FF, Opera …). The demo takes care of that.





Entries (RSS)