Capturing Special Keys on your DataWindow
Posted In:
DataWindow
.
By popo
A common requirement for applications is to capture certain keystrokes and perform special
actions when the user presses these keys. To capture keystrokes on your DataWindow, do the
following:
- Declare an event on your DataWindow (possibly your ancestor DW, as is done here) and assign that event the pbm_dwnkey event id. This causes all keystrokes to be processed through the event.
- Code that event to handle the special keystrokes in the desired manner. Ideally, common keystrokes (like Enter, Tab) will be captured at the ancestor level and a standard event will be called. A sample event that catches certain keystrokes is shown below.
- tabs: to do so, a descendant DW sets the ib_CaptureTab instance variable to True. The
ue_Tab or ue_BackTab event are then invoked when Tab/BackTab is pressed to allow
the descendant to code appropriate logic. - enter keys: to do so, a descendant DW sets the ib_CaptureEnter instance variable to True.
The ue_Enter event is then called when Enter is pressed to allow the descendant to code
appropriate logic.
// if descendent DW wants tab ... capture it
IF ib_CaptureTab AND KeyDown(KeyTab!) THEN
// determine whether it is a tab or back-tab
IF NOT KeyDown(KeyShift!) THEN
This.POST ue_Tab()
ELSE
This.POST ue_BackTab()
END IF
Return 1 // have key not otherwise be ignored by PB
// if descendent DW wants enter keys ... capture it
ELSEIF ib_CaptureEnter AND KeyDown(KeyEnter!) THEN
This.POST ue_Enter()
Return 1 // have key not otherwise be ignored by PB
// otherwise send the key to the window-level for standard processing
ELSE
IF IsValid(iw_ParentWin) THEN
iw_ParentWin.TriggerEvent(Key!)
ELSE
// how did we get a key event for invalid parent?
f_SignalError(0/gi_zero,"Invalid Parent Window in Key Event")
END IF
END IF
Note: a handle to the parent window (iw_ParentWin) is obtained in the Constructor event. PFC's
of_GetParentWindow function is a good method to generically get a handle to the parent
window.