You should be able to do this using SendInput
with the KEYEVENTF_UNICODE
flag. This bypasses the problems discussed in the comments about different keyboards using different scan codes to represent the number sign. It has a universally recognized Unicode representation.
The KEYEVENTF_UNICODE
flag should be specified in the dwFlags
member of the KEYBDINPUT
structure that you pass to the SendInput
function. When you use this flag, you set the wVk
member to 0 and use the wScan
member instead. The documentation for that structure contains more detailed information.
But it should be pretty simple. Any properly-designed app is going to obtain the message (using either GetMessage
or PeekMessage
), and then call TranslateMessage
, which will cause a WM_CHAR
message to be posted with the appropriate Unicode character value. It even handles the case where you're dealing with a legacy, non-Unicode application: if the message is posted to an ANSI window, the Unicode value will be automatically converted to the appropriate ANSI value. This shouldn't be a problem, all character sets I can think of include a number sign.
Alternatively, you could probably do the translation yourself using the VkKeyScanEx
function to obtain the virtual-key code of the character, and then mapping this virtual-key code to a scan code using MapVirtualKeyEx
. I can't say for sure if this will work, though, as I've never tried this approach.