瀏覽代碼

* 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 年之前
父節點
當前提交
b5575c0829
共有 1 個文件被更改,包括 31 次插入29 次删除
  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;
 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;
 
-procedure UpdateKeyboardLayoutInfo;
 begin
-  KeyBoardLayout:=GetKeyboardLayout(0);
-  CheckAltGr;
+  NewKeyBoardLayout:=GetKeyboardLayout(0);
+  if force or (NewKeyboardLayout <> KeyBoardLayout) then
+    begin
+      KeyBoardLayout:=NewKeyboardLayout;
+      CheckAltGr;
+    end;
 end;
 
 { 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
     we must check and update our idea of the current keyboard layout on every
     key event we receive. :( }
-  UpdateKeyboardLayoutInfo;
+  UpdateKeyboardLayoutInfo(False);
 
   with ir.Event.KeyEvent do
     begin
@@ -355,7 +358,7 @@ end;
 
 procedure SysInitKeyboard;
 begin
-   KeyBoardLayout:=GetKeyboardLayout(0);
+   UpdateKeyboardLayoutInfo(True);
    lastShiftState := 0;
    FlushConsoleInputBuffer(StdInputHandle);
    newKeyEvent := CreateEvent (nil,        // address of security attributes
@@ -373,7 +376,6 @@ begin
 
    nextkeyevent:=0;
    nextfreekeyevent:=0;
-   checkaltgr;
    SetKeyboardEventHandler (@HandleKeyboard);
    Inited:=true;
 end;