Page 1 of 1
Using PRIM_KBRD
Posted: Wed Sep 11, 2024 1:27 am
by RomainG
Hello,
I am looking for a way to check if a key is pressed inside an eventroutine and I have found the PRIM_KBRD component class which provides access to the Shift, Alt and Ctrl key states. However, when I try to define it in a view component, I have this error: "Component class PRIM_KBRD is restricted and cannot be used in component MYVIEW".
I have looked on the forum and the documentation but there is nothing on this issue.
Any ideas ?
Thanks,
Romain
Re: Using PRIM_KBRD
Posted: Wed Sep 11, 2024 9:22 am
by BrendanB
Romain,
There is a Keypress event on the view component.
Code: Select all
Evtroutine Handling(#COM_OWNER.KeyPress) Char(#Char) Handled(#Handled) IsAltDown(#IsAltDown) IsControlDown(#IsControlDown) IsShiftDown(#IsShiftDown) KeyCode(#Keycode)
* Here is where you can either process or call a method to process what key/key combination was pressed
#Handled := #com_self.HandleKeyPress(#Char #IsAltDown #IsControlDown #IsShiftDown #Keycode)
* make sure that you return a Boolean to indicate if you are handling this keypress.
Endroutine
Mthroutine Name(HandleKeyPress)
Define_Map For(*INPUT) Class(#PRIM_ALPH) Name(#Char)
Define_Map For(*INPUT) Class(#PRIM_BOLN) Name(#AltIsDown)
Define_Map For(*INPUT) Class(#PRIM_BOLN) Name(#ControlIsDown)
Define_Map For(*INPUT) Class(#PRIM_BOLN) Name(#ShiftIsDown)
Define_Map For(*INPUT) Class(#PRIM_ALPH) Name(#KeyCode)
Define_Map For(*RESULT) Class(#PRIM_BOLN) Name(#Result)
If (#KeyCode = 'IsChar')
* Handle as a Character...
Else
* #KeyCode is one of the control keys (such as Enter, UpArrow, F1 etc... - press F2 on #Keycode in eventroutine to see
* the possible list)
Endif
Endroutine
I note that the help will direct you to:
https://docs.lansa.com/15/en/lansa016/c ... yPress.htm
which is because #PRIM_VIEW *Extends #PRIM_PANL
this is good, because it also means that the same event is available in Forms...
The downside to using this event: without some testing, i can not be sure if this will receive the keypress BEFORE or AFTER an input field that may be on the view.... (remembering that input fields such as an Edit box will also have a KeyPress event )
I suspect that you Edit (if it has focus) would get the keypress and so would need to set #Handled := False if it was a keypress that should be handled by the page...
Hope that helps a little...
Re: Using PRIM_KBRD
Posted: Wed Sep 11, 2024 6:29 pm
by RomainG
Hi Brendan,
I have tried using the keypress event but for some reason the event is not fired when I press the control or shift keys. It is fired when I press other keys though.
Below is part of my code:
Code: Select all
Define_Com Class(#PRIM_LIST) Name(#Categories) Parent(#COM_OWNER) Displayposition(4) Tabposition(4) Height(320) Width(310) Top(130) Left(0) Selectionstyle(All)
Evtroutine Handling(#Categories.KeyPress) Keycode(#Key) Iscontroldown(#isControlDown) isShiftDown(#isShiftDown)
#sys_web.console.log( #Key )
#sys_web.console.log( #isControlDown )
#controlState := #isControlDown
#shiftState := #isShiftDown
Endroutine
Thanks,
Romain
Re: Using PRIM_KBRD
Posted: Thu Sep 12, 2024 9:33 am
by BrendanB
Romain,
I used the following view for my testing: (compile the view, and show it on a webpage).
what i observed is that:
1. SHIFT, CONTROL, ALT all *only show* when another key is pressed.
2. you cannot press combinations that the Browser already checks for (eg. CONTROL-C, CONTROL-F, CONTROL-SHIFT-I etc)
I tested using:
Control-Q
Control-Shift-Q
Shift-Q
Alt-F7
Control-Alt-F7
Control-Shift-Alt-F7
NOTE: Alt+ a char or number is interpreted by the OperatingSystem to input unicode characters...
so you must ensure that you are wanting a combination that is not used by anything else.
Brendan.
Code: Select all
Begin_Com Role(*EXTENDS #PRIM_VIEW) DisplayPosition(1) TabPosition(1) LayoutManager(#Layout1) Caption('test1') Height(673) Width(820) ThemeDrawStyle('Card')
Define_Com Class(#PRIM_TBLO) Name(#Layout1)
Define_Com Class(#PRIM_TBLO.Column) Name(#Layout1Column2) DisplayPosition(2) Parent(#Layout1)
Define_Com Class(#PRIM_TBLO.Row) Name(#Layout1Row1) DisplayPosition(1) Parent(#Layout1) Height(40) Units(Content) MinHeight(56)
Define_Com Class(#PRIM_TBLO.Column) Name(#Layout1Column1) DisplayPosition(1) Parent(#Layout1)
Define_Com Class(#PRIM_TBLO.Row) Name(#Layout1Row2) DisplayPosition(2) Parent(#Layout1)
Define_Com Class(#PRIM_TBLO.Item) Name(#Layout1Item1) Alignment(CenterLeft) Column(#Layout1Column1) Manage(#Text) Parent(#Layout1) Row(#Layout1Row1) Sizing(ContentHeight) ColumnSpan(2) Flow(Down)
Define_Com Class(#PRIM_TBLO.Item) Name(#Layout1Item2) Alignment(TopLeft) Column(#Layout1Column2) Manage(#controlLabel) Parent(#Layout1) Row(#Layout1Row2) Sizing(FitToWidth) Flow(Down)
Define_Com Class(#PRIM_TBLO.Item) Name(#Layout1Item3) Alignment(TopLeft) Column(#Layout1Column2) Manage(#shiftLabel) Parent(#Layout1) Row(#Layout1Row2) Sizing(FitToWidth) Flow(Down) MarginTop(8)
Define_Com Class(#PRIM_TBLO.Item) Name(#Layout1Item4) Alignment(TopLeft) Column(#Layout1Column2) Manage(#altLabel) Parent(#Layout1) Row(#Layout1Row2) Sizing(FitToWidth) Flow(Down) MarginTop(8)
Define_Com Class(#PRIM_TBLO.Item) Name(#Layout1Item5) Alignment(TopLeft) Column(#Layout1Column2) Manage(#keyCodeLabel) Parent(#Layout1) Row(#Layout1Row2) Sizing(FitToWidth) Flow(Down) MarginTop(8)
Define_Com Class(#PRIM_TBLO.Item) Name(#Layout1Item6) Alignment(TopLeft) Column(#Layout1Column2) Manage(#charLabel) Parent(#Layout1) Row(#Layout1Row2) Sizing(FitToWidth) Flow(Down) MarginTop(8)
Define_Com Class(#PRIM_TBLO) Name(#LayoutList)
Define_Com Class(#PRIM_TBLO.Column) Name(#LayoutListColumn1) DisplayPosition(1) Parent(#LayoutList)
Define_Com Class(#PRIM_TBLO.Row) Name(#LayoutListRow1) DisplayPosition(1) Parent(#LayoutList)
Define_Com Class(#PRIM_MD.Label) Name(#Text) Caption('KeyPress Tester') DisplayPosition(1) Left(0) Parent(#COM_OWNER) TabPosition(1) ThemeDrawStyle('Heading2+ForegroundMediumPrimary') Top(8) Width(820) Height(40) WordWrap(True) Ellipses(False) CaptionMarginBottom(8) CaptionMarginTop(8) CaptionMarginLeft(8) CaptionMarginRight(8)
Define_Com Class(#PRIM_MD.Label) Name(#controlLabel) Caption('Control Is Down') DisplayPosition(2) Left(409) Parent(#COM_OWNER) TabPosition(2) Top(56) Width(409) IconAlignment(CenterRight) Icon('cancel') PaddingLeft(16) PaddingRight(16) ThemeDrawStyle('Card')
Define_Com Class(#PRIM_MD.Label) Name(#shiftLabel) Caption('Shift Is Down') DisplayPosition(3) Left(409) Parent(#COM_OWNER) TabPosition(3) Top(100) Width(409) ThemeDrawStyle('Card') PaddingLeft(16) PaddingRight(16) Icon('cancel') IconAlignment(CenterRight)
Define_Com Class(#PRIM_MD.Label) Name(#altLabel) Caption('Alt Is Down') DisplayPosition(4) Left(409) Parent(#COM_OWNER) TabPosition(4) Top(144) Width(409) ThemeDrawStyle('Card') Icon('cancel') IconAlignment(CenterRight) PaddingLeft(16) PaddingRight(16)
Define_Com Class(#PRIM_MD.Label) Name(#keyCodeLabel) Caption('KeyCode') DisplayPosition(5) Left(409) Parent(#COM_OWNER) TabPosition(5) Top(188) Width(409) ThemeDrawStyle('Card') Icon('keyboard') IconAlignment(CenterRight) PaddingLeft(16) PaddingRight(16)
Define_Com Class(#PRIM_MD.Label) Name(#charLabel) Caption('Text') DisplayPosition(6) Left(409) Parent(#COM_OWNER) TabPosition(6) Top(232) Width(409) ThemeDrawStyle('Card') PaddingLeft(16) PaddingRight(16) CaptionMarginBottom(8) CaptionMarginTop(8) Ellipses(False) WordWrap(True)
Evtroutine Handling(#COM_OWNER.Prepare)
#COM_SELF.SetFocus
Endroutine
Evtroutine Handling(#Com_owner.KeyPress) Handled(#Handled) Char(#Char) IsAltDown(#IsAltDown) IsControlDown(#IsControlDown) IsShiftDown(#IsShiftDown) KeyCode(#KeyCode)
#keyCodeLabel.Caption := #KeyCode
#charLabel.Caption := #Char
#altLabel.ThemeDrawStyle #shiftLabel.ThemeDrawStyle #controlLabel.ThemeDrawStyle := 'Card'
If (#IsAltDown)
#altLabel.ThemeDrawStyle := 'Card+ForegroundMediumSuccess'
Endif
If (#IsShiftDown)
#shiftLabel.ThemeDrawStyle := 'Card+ForegroundMediumSuccess'
Endif
If (#IsControlDown)
#controlLabel.ThemeDrawStyle := 'Card+ForegroundMediumSuccess'
Endif
Endroutine
End_Com