Browse Source

Minor bug fixes in keytest.pas. Issue #41473.

Margers 1 month ago
parent
commit
b584df7774
1 changed files with 28 additions and 23 deletions
  1. 28 23
      packages/fv/examples/keytest.pas

+ 28 - 23
packages/fv/examples/keytest.pas

@@ -22,9 +22,11 @@ TYPE
   TKeyInfoView = OBJECT(TView)
   TKeyInfoView = OBJECT(TView)
     LastKeyEvent: TEvent; { Store the last event here }
     LastKeyEvent: TEvent; { Store the last event here }
     TVInputValue: UnicodeString;
     TVInputValue: UnicodeString;
+    Cnt: Integer;
     CONSTRUCTOR Init(VAR Bounds: TRect);
     CONSTRUCTOR Init(VAR Bounds: TRect);
     PROCEDURE Draw; VIRTUAL;
     PROCEDURE Draw; VIRTUAL;
     PROCEDURE UpdateInfo(CONST Event: TEvent);
     PROCEDURE UpdateInfo(CONST Event: TEvent);
+    PROCEDURE HandleEvent(VAR Event: TEvent); VIRTUAL;
   END;
   END;
 
 
   PKeyTestApp = ^TKeyTestApp;
   PKeyTestApp = ^TKeyTestApp;
@@ -34,14 +36,10 @@ TYPE
   TKeyTestApp = OBJECT(TApplication)
   TKeyTestApp = OBJECT(TApplication)
     KeyInfoView: PKeyInfoView; { Pointer to our view for displaying information }
     KeyInfoView: PKeyInfoView; { Pointer to our view for displaying information }
     CONSTRUCTOR Init;
     CONSTRUCTOR Init;
-    PROCEDURE HandleEvent(VAR Event: TEvent); VIRTUAL;
     PROCEDURE InitMenuBar; VIRTUAL;
     PROCEDURE InitMenuBar; VIRTUAL;
     PROCEDURE InitStatusLine; VIRTUAL;
     PROCEDURE InitStatusLine; VIRTUAL;
   END;
   END;
 
 
-VAR
-  Cnt: Integer;
-
 {---------------------------------------------------------------------------}
 {---------------------------------------------------------------------------}
 {                        TKeyInfoView OBJECT METHODS                        }
 {                        TKeyInfoView OBJECT METHODS                        }
 {---------------------------------------------------------------------------}
 {---------------------------------------------------------------------------}
@@ -50,9 +48,11 @@ CONSTRUCTOR TKeyInfoView.Init(VAR Bounds: TRect);
 BEGIN
 BEGIN
   Inherited Init(Bounds);
   Inherited Init(Bounds);
   Options := Options OR ofSelectable; { Make the View selectable so it can get focus }
   Options := Options OR ofSelectable; { Make the View selectable so it can get focus }
+  GrowMode := gfGrowHiX + gfGrowHiY;  { Grow along with window resize }
   EventMask := $FFFF;                 { Accept all event types }
   EventMask := $FFFF;                 { Accept all event types }
   FillChar(LastKeyEvent, SizeOf(TEvent), 0); { Initialize with zeros }
   FillChar(LastKeyEvent, SizeOf(TEvent), 0); { Initialize with zeros }
   LastKeyEvent.What := evNothing;     { No events initially }
   LastKeyEvent.What := evNothing;     { No events initially }
+  Cnt := 0;
 END;
 END;
 
 
 { Function to format the modifier key state byte into a readable string }
 { Function to format the modifier key state byte into a readable string }
@@ -78,6 +78,8 @@ VAR
   Line: UnicodeString;
   Line: UnicodeString;
   Y: Integer;
   Y: Integer;
   Color: Byte;
   Color: Byte;
+  LastKeyCharCode: AnsiChar;
+  LastKeyUnicodeChar: WideChar;
 BEGIN
 BEGIN
   Color := GetColor(1);
   Color := GetColor(1);
   { Fill the view's background with spaces using the current color }
   { Fill the view's background with spaces using the current color }
@@ -97,8 +99,6 @@ BEGIN
     Exit;
     Exit;
   END;
   END;
 
 
-  Cnt := Cnt + 1;
-
   { Display all information from the TEvent record }
   { Display all information from the TEvent record }
   Line := Format('Event.What: $%4.4x (evKeyDown)', [LastKeyEvent.What]);
   Line := Format('Event.What: $%4.4x (evKeyDown)', [LastKeyEvent.What]);
   MoveStr(B, Line, Color);
   MoveStr(B, Line, Color);
@@ -108,7 +108,10 @@ BEGIN
   MoveStr(B, Line, Color);
   MoveStr(B, Line, Color);
   WriteLine(1, 2, StrWidth(Line), 1, B);
   WriteLine(1, 2, StrWidth(Line), 1, B);
 
 
-  Line := Format('CharCode:    ''%s'' ($%2.2x)', [LastKeyEvent.CharCode, Ord(LastKeyEvent.CharCode)]);
+  LastKeyCharCode:=LastKeyEvent.CharCode;
+  IF LastKeyCharCode < #32 THEN
+    LastKeyCharCode:=#32;   { Non displayable chars are shown as space }
+  Line := Format('CharCode:    ''%s'' ($%2.2x)', [LastKeyCharCode, Ord(LastKeyEvent.CharCode)]);
   MoveStr(B, Line, Color);
   MoveStr(B, Line, Color);
   WriteLine(1, 3, StrWidth(Line), 1, B);
   WriteLine(1, 3, StrWidth(Line), 1, B);
 
 
@@ -116,7 +119,10 @@ BEGIN
   MoveStr(B, Line, Color);
   MoveStr(B, Line, Color);
   WriteLine(1, 4, StrWidth(Line), 1, B);
   WriteLine(1, 4, StrWidth(Line), 1, B);
 
 
-  Line := Format('UnicodeChar: ''%s'' (U+%4.4x)', [LastKeyEvent.UnicodeChar, Ord(LastKeyEvent.UnicodeChar)]);
+  LastKeyUnicodeChar:=LastKeyEvent.UnicodeChar;
+  IF LastKeyUnicodeChar < #32 THEN
+    LastKeyUnicodeChar:=#32;   { Non displayable chars are shown as space }
+  Line := Format('UnicodeChar: ''%s'' (U+%4.4x)', [LastKeyUnicodeChar, Ord(LastKeyEvent.UnicodeChar)]);
   MoveStr(B, Line, Color);
   MoveStr(B, Line, Color);
   WriteLine(1, 5, StrWidth(Line), 1, B);
   WriteLine(1, 5, StrWidth(Line), 1, B);
 
 
@@ -144,9 +150,23 @@ END;
 PROCEDURE TKeyInfoView.UpdateInfo(CONST Event: TEvent);
 PROCEDURE TKeyInfoView.UpdateInfo(CONST Event: TEvent);
 BEGIN
 BEGIN
   LastKeyEvent := Event;
   LastKeyEvent := Event;
+  Cnt := Cnt + 1;
   DrawView; { Request a redraw }
   DrawView; { Request a redraw }
 END;
 END;
 
 
+PROCEDURE TKeyInfoView.HandleEvent(VAR Event: TEvent);
+BEGIN
+  { Call the ancestor's handler }
+  Inherited HandleEvent(Event);
+
+  { If the event is a key press, update the info in our View }
+  IF Event.What = evKeyDown THEN
+  BEGIN
+    UpdateInfo(Event);
+    ClearEvent(Event);    { Clear the event }
+  END;
+END;
+
 {---------------------------------------------------------------------------}
 {---------------------------------------------------------------------------}
 {                        TKeyTestApp OBJECT METHODS                         }
 {                        TKeyTestApp OBJECT METHODS                         }
 {---------------------------------------------------------------------------}
 {---------------------------------------------------------------------------}
@@ -175,20 +195,6 @@ BEGIN
   DeskTop^.Insert(MainWindow);
   DeskTop^.Insert(MainWindow);
 END;
 END;
 
 
-PROCEDURE TKeyTestApp.HandleEvent(VAR Event: TEvent);
-BEGIN
-  { First, call the ancestor's handler so standard things like menus work }
-  Inherited HandleEvent(Event);
-
-  { If the event is a key press, update the info in our View }
-  IF Event.What = evKeyDown THEN
-  BEGIN
-    IF Assigned(KeyInfoView) THEN
-      KeyInfoView^.UpdateInfo(Event);
-    { Don't clear the event, so standard handlers (like Alt+X) also get to process it }
-  END;
-END;
-
 PROCEDURE TKeyTestApp.InitMenuBar;
 PROCEDURE TKeyTestApp.InitMenuBar;
 VAR R: TRect;
 VAR R: TRect;
 BEGIN
 BEGIN
@@ -223,7 +229,6 @@ end;
 VAR
 VAR
   MyApp: TKeyTestApp;
   MyApp: TKeyTestApp;
 BEGIN
 BEGIN
-  Cnt := 0;
   MyApp.Init;
   MyApp.Init;
   MyApp.Run;
   MyApp.Run;
   MyApp.Done;
   MyApp.Done;