1

i have a problem with using sendkeys powershell method, so when i type this code in the console:

$obj = New-Object -com Wscript.Shell

$obj.SendKeys("a")

with keyboard configured in french language, i have the character a displayed, but when i turn the language of the keyboard to Arabic (Algeria), i get the character ش displayed instead of ض, it looks like the keyboard change from azerty configuration to qwerty. also, i've tried to change arabic configuration of the keyboard to arabic (102) AZERTY, arabic (102) and arabic (101) can someone help me please.

Mikomalik
  • 11
  • 3
  • This might solve your issue: `$enc = [System.Text.Encoding]::UTF8` > `$char=$enc.GetString($enc.GetBytes('ش'))` > `$obj.SendKeys($char)`. Someone had a similar issue in this post: https://stackoverflow.com/questions/66701631/powershell-unicode-characters-transforming-unexpectedly/66701778#66701778 – Santiago Squarzon Mar 25 '21 at 20:05
  • 2
    The [`SendKeys` method](https://social.technet.microsoft.com/wiki/contents/articles/5169.vbscript-sendkeys-method.aspx) is used to send keystrokes to the currently active window **as if they where typed from the keyboard**. So `SendKeys` uses [Keyboard Scan Codes](https://www.millisecond.com/support/docs/v6/html/language/scancodes.htm) rather than (unicode) characters… – JosefZ Mar 25 '21 at 20:38

1 Answers1

1

You can try the SendWait method from .NET:

[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
[System.Windows.Forms.SendKeys]::SendWait("x")

From this answer by @wp78de.

If that fails, you may need to use some other methods:

This answer walks through changing your keyboard language temporarily to send text.

This answer suggests using win32 SendMessage API methods instead of emulated key presses.

Cpt.Whale
  • 4,784
  • 1
  • 10
  • 16
  • Unfortunately, `SendWait` sends Keyboard Scan Codes rather than (unicode) characters… (the same as _ComObject_ `Wscript.Shell` (see [another comment](https://stackoverflow.com/questions/66806701/powershell-sendkeys-doesnt-work-properly-with-arabic-character#comment118094434_66806701)) – JosefZ Mar 25 '21 at 20:42