Browse Source

* optimized UpdateKeyboardLayoutInfo, so it only calls GetKeyboardLayout once
and only performs the HasAltGr check in case the keyboard layout has actually
changed

git-svn-id: branches/unicodekvm@41607 -

nickysn 6 years ago
parent
commit
b5575c0829
1 changed files with 31 additions and 29 deletions
  1. 31 29
      packages/rtl-console/src/win/keyboard.pp

+ 31 - 29
packages/rtl-console/src/win/keyboard.pp

@@ -149,37 +149,40 @@ begin
   transShiftState := b;
   transShiftState := b;
 end;
 end;
 
 
-procedure CheckAltGr;
-
-var ahkl : HKL;
-    i    : integer;
+procedure UpdateKeyboardLayoutInfo(Force: Boolean);
+var
+  NewKeyboardLayout: HKL;
 
 
- begin
-   HasAltGr:=false;
+  procedure CheckAltGr;
+  var i: integer;
+  begin
+    HasAltGr:=false;
 
 
-   ahkl:=GetKeyboardLayout(0);
-   i:=$20;
-   while i<$100 do
-     begin
-       // <MSDN>
-       // For keyboard layouts that use the right-hand ALT key as a shift key
-       // (for example, the French keyboard layout), the shift state is
-       // represented by the value 6, because the right-hand ALT key is
-       // converted internally into CTRL+ALT.
-       // </MSDN>
-      if (HIBYTE(VkKeyScanEx(chr(i),ahkl))=6) then
-        begin
-          HasAltGr:=true;
-          break;
-        end;
-     inc(i);
+    i:=$20;
+    while i<$100 do
+      begin
+        // <MSDN>
+        // For keyboard layouts that use the right-hand ALT key as a shift key
+        // (for example, the French keyboard layout), the shift state is
+        // represented by the value 6, because the right-hand ALT key is
+        // converted internally into CTRL+ALT.
+        // </MSDN>
+        if (HIBYTE(VkKeyScanEx(chr(i),KeyBoardLayout))=6) then
+          begin
+            HasAltGr:=true;
+            break;
+          end;
+      inc(i);
     end;
     end;
-end;
+  end;
 
 
-procedure UpdateKeyboardLayoutInfo;
 begin
 begin
-  KeyBoardLayout:=GetKeyboardLayout(0);
-  CheckAltGr;
+  NewKeyBoardLayout:=GetKeyboardLayout(0);
+  if force or (NewKeyboardLayout <> KeyBoardLayout) then
+    begin
+      KeyBoardLayout:=NewKeyboardLayout;
+      CheckAltGr;
+    end;
 end;
 end;
 
 
 { The event-Handler thread from the unit event will call us if a key-event
 { The event-Handler thread from the unit event will call us if a key-event
@@ -254,7 +257,7 @@ begin
     changes, but unfortunately, console apps get no such notification. Therefore
     changes, but unfortunately, console apps get no such notification. Therefore
     we must check and update our idea of the current keyboard layout on every
     we must check and update our idea of the current keyboard layout on every
     key event we receive. :( }
     key event we receive. :( }
-  UpdateKeyboardLayoutInfo;
+  UpdateKeyboardLayoutInfo(False);
 
 
   with ir.Event.KeyEvent do
   with ir.Event.KeyEvent do
     begin
     begin
@@ -355,7 +358,7 @@ end;
 
 
 procedure SysInitKeyboard;
 procedure SysInitKeyboard;
 begin
 begin
-   KeyBoardLayout:=GetKeyboardLayout(0);
+   UpdateKeyboardLayoutInfo(True);
    lastShiftState := 0;
    lastShiftState := 0;
    FlushConsoleInputBuffer(StdInputHandle);
    FlushConsoleInputBuffer(StdInputHandle);
    newKeyEvent := CreateEvent (nil,        // address of security attributes
    newKeyEvent := CreateEvent (nil,        // address of security attributes
@@ -373,7 +376,6 @@ begin
 
 
    nextkeyevent:=0;
    nextkeyevent:=0;
    nextfreekeyevent:=0;
    nextfreekeyevent:=0;
-   checkaltgr;
    SetKeyboardEventHandler (@HandleKeyboard);
    SetKeyboardEventHandler (@HandleKeyboard);
    Inited:=true;
    Inited:=true;
 end;
 end;