Hi,
Michael said:
Hi,
I want to get a char from a virtual key, and if SHIFT key or Alt Gr key
is
pressed get the char corresponding ( the char in upper case for virtual
keys
between a and z).
I saw the ToAscii API function, and use it, but always ignore SHIFT key.
I
thought that with the KeyboardState param read the shift, alt keys state
and
return the corresponding char, but does not work.
How can i do what i want?
You're not telling us how you are currently doing it, i'm using something
like this:
using System;
using System.Windows.Forms;
using System.Text;
using System.Runtime.InteropServices;
[DllImport("user32.dll", SetLastError=true)]
public static extern int ToAscii(
uint uVirtKey, // virtual-key code
uint uScanCode, // scan code
byte[] lpKeyState, // key-state array
StringBuilder lpChar, // buffer for translated key
uint flags // active-menu flag
);
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
byte[] keysDown = new byte[256];
keysDown[(int)Keys.ShiftKey] = 0x80; // SHIFT down
// keysDown[(int)Keys.Menu] = 0x80; // ALT down
// keysDown[(int)Keys.ControlKey] = 0x80; // CONTROL down
StringBuilder sb = new StringBuilder(2);
int i = ToAscii((uint)Keys.A, 0, keysDown, sb, 0);
switch (i)
{
case -1:
// dead key
Console.WriteLine("Dead Key, Spacing version={0}",
sb.ToString()[0]);
// warning!: though the spacing version is returned, the
non-spacing
// version is still in the keyboard buffer, therefore it's
recommended
// to call ToAscii again with a non-dead key to form a combined
character.
// (as this will remove the deadkey from the keyb buffer)
break;
case 0:
// no character
Console.WriteLine("No character");
break;
case 1:
// character (combined or not)
Console.WriteLine("Character={0}", sb.ToString()[0] );
break;
case 2:
// deadkey (from previous call) + base (this call) which doesn't
combine
Console.WriteLine("Uncombinable deadkey={0} base={1}",
sb.ToString()[0], sb.ToString()[1] );
break;
}
Console.ReadLine();
}
HTH,
greetings
Thanks