Recognize key strokes in string entry

Hello,
I wonder if there is a way to recognize each key stroke while writing inside the entry field in order to validate evey letter while typing.

Regards

I tried with “IMM” attribute but it didn’t work. may be I am not using it the proper way.

If ENTRY has the IMM attribute, the RTL posts the EVENT:NewSelection event for every keystroke changing position of the caret or a character on the caret position. Functions KEYCODE() and KEYCHAR() can be called during processing of this event to find the pressed keystroke.

BUT… If you’re wanting to control the input, you must have in mind a lot of nuances: presence of the MASK attribute, special keystrokes like Ctrl-C or Ctrl-Z, etc.

Thank you. that was helpful.

Here is a simple test program demonstrating work with the IMM ENTRY control.
test.clw (2.4 KB)

1 Like

Thank you. so appreciated.

Regards

Code excerpt from Test.clw to take a KEYCODE() Long and make a human readable string like ‘Ctrl+Shift-p’:

    Q.KChar = CharToHex (KEYCHAR())
    Q.KCode = CodeToHex (KEYCODE())   !<-- Takes Keycode make Human readable
...
CodeToHex  PROCEDURE (LONG V)
S      CSTRING(33)
  CODE
  IF BAND (V, AltKeyPressed) <> 0       !AltKeyPressed EQUATE(0400h)
    V = BXOR (V, AltKeyPressed)
    S = 'Alt-'
  END
  IF BAND (V, CtrlKeyPressed) <> 0      !CtrlKeyPressed EQUATE(0200h)
    V = BXOR (V, CtrlKeyPressed)
    S = S & 'Ctrl-'
  END
  IF BAND (V, ShiftKeyPressed) <> 0     !ShiftKeyPressed EQUATE(0100h)
    V = BXOR (V, ShiftKeyPressed)
    S = S & 'Shift-'
  END
  S = S & CharToHex (V)             !<< === ????? Correct ????
  RETURN S        

CharToHex  PROCEDURE (LONG V)
  CODE
  IF V >= 20h AND V < 7Fh THEN      !<< === ????? Correct ????
    RETURN CHR(V)
  END
  RETURN ToHex (V)                      

The Test.clw is just a quick example to show IMM working and for that it is perfectly fine. Its very slick and small code.

I’d mention KEYCODE() does not map to Characters in a straight line so the code IF V >= 20h AND V < 7Fh THEN is not quite correct e.g. pressing on the Number Pad “1,2,3,4,5,6,7,8,9” will show key pressed as “a,b,c,d,e,f,g,h,i”. The OP will want to use KEYCHAR() to spot problems which will not be affected.

I use code like this to spot some special ranges of KeyCodes like Number Pad is 60h to 69h:

ClaKeyCodeExplain   PROCEDURE(LONG K)!,STRING
HB      BYTE,AUTO           !Char as Byte
HC      STRING(1),OVER(HB)  !CHAR
KN      PSTRING(32)
    CODE
    HB    = BAND(K,0FFh) 
    CASE HB                                      !Make a Key Name
    OF 30h TO 39h ; KN = 'Key' & HC              !Key0 - Key9
    OF 41h TO 5Ah ; KN = HC & 'Key'              !AKey - ZKey
    OF 60h TO 69h ; KN = 'KeyPad' & HB-60h       !KeyPad0  - KeyPad9 
    OF 70h TO 7Bh ; KN = 'F' & HB-70H+1 & 'Key'  !F1Key - F12Key 7Bh - F13-F24
    ELSE
        KN=CodeToHex(HB) &'h'   !See below check unknown
    END !CASE HB
    IF K > 255 THEN 
        KN = CHOOSE(~BAND(K,400h),'','Alt+')   & |
             CHOOSE(~BAND(K,200h),'','Ctrl+')  & |
             CHOOSE(~BAND(K,100h),'','Shift+') & KN
    END
    RETURN KN

!Could check unknown HEX against this list:
KeyEQT  STRING(' 01MouseLeft 02MouseRight 03MouseCenter 05MouseLeft2 06MouseRight2 07MouseCenter2 ' &|
         '81MouseLeftUp 82MouseRightUp 83MouseCenterUp 08Backspace 08BS 09Tab 0DEnter 13Pause 14CapsLock ' &|
         '1BEsc 20Space 21PgUp 22PgDn 23End 24Home 25Left 26Up 27Right 28Down 2CPrint 2DInsert ' &|
         '2EDelete 5BWinLeft 5CWinRight 5DApps 6AAst 6BPlus 6DMinus 6EDecimal 6FSlash ')