Skip to content

Commit

Permalink
fixed: hotkey is not correctly parsed in variant culture
Browse files Browse the repository at this point in the history
  • Loading branch information
d2phap committed Nov 10, 2024
1 parent f869b87 commit c484f0a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
1 change: 0 additions & 1 deletion Source/Components/ImageGlass.Base/NativeMethods.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,4 @@ ShellExecuteEx
StrCmpLogicalW
GetWindowPlacement
MapVirtualKey
GetKeyboardLayout
VkKeyScanEx
39 changes: 25 additions & 14 deletions Source/Components/ImageGlass.Base/WinApi/KeyboardApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using System.Globalization;
using System.Runtime.InteropServices;
using Windows.Win32;
using Windows.Win32.UI.Input.KeyboardAndMouse;
Expand All @@ -36,11 +35,6 @@ public class KeyboardApi
/// </summary>
public static unsafe char KeyCodeToChar(Keys key, bool withShiftKey)
{
CultureInfo.DefaultThreadCurrentCulture =
CultureInfo.DefaultThreadCurrentUICulture =
Thread.CurrentThread.CurrentCulture =
Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;

var lpChar = 0u;
var lpKeyState = new byte[256];

Expand All @@ -55,9 +49,21 @@ public static unsafe char KeyCodeToChar(Keys key, bool withShiftKey)
}
}

// always use en-US keyboard layout
nint langHandle;
try
{
var culture = new System.Globalization.CultureInfo("en-US");
langHandle = InputLanguage.FromCulture(culture).Handle;
}
catch
{
langHandle = InputLanguage.DefaultInputLanguage.Handle;
}

var keyboardLayoutPtr = new HKL(langHandle);
var virtualKeyCode = (uint)key;
var scanCode = PInvoke.MapVirtualKey(virtualKeyCode, MAP_VIRTUAL_KEY_TYPE.MAPVK_VK_TO_VSC);
var keyboardLayoutPtr = PInvoke.GetKeyboardLayout(0);

_ = ToAsciiEx((int)key, (int)scanCode, lpKeyState, ref lpChar, 0, keyboardLayoutPtr);

Expand All @@ -70,14 +76,19 @@ public static unsafe char KeyCodeToChar(Keys key, bool withShiftKey)
/// </summary>
public static Keys CharToKeyCode(char c)
{
CultureInfo.DefaultThreadCurrentCulture =
CultureInfo.DefaultThreadCurrentUICulture =
Thread.CurrentThread.CurrentCulture =
Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;


var keyboardLayoutPtr = PInvoke.GetKeyboardLayout(0);
// always use en-US keyboard layout
nint langHandle;
try
{
var culture = new System.Globalization.CultureInfo("en-US");
langHandle = InputLanguage.FromCulture(culture).Handle;
}
catch
{
langHandle = InputLanguage.DefaultInputLanguage.Handle;
}

var keyboardLayoutPtr = new HKL(langHandle);
var vkey = PInvoke.VkKeyScanEx(c, keyboardLayoutPtr);
var keys = (Keys)(vkey & 0xff);
var modifiers = vkey >> 8;
Expand Down

0 comments on commit c484f0a

Please sign in to comment.