Guest User

Untitled

a guest
Jun 23rd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. using System.Runtime.InteropServices;
  2. public static class User32Interop
  3. {
  4. public static char ToAscii(Keys key, Keys modifiers)
  5. {
  6. var outputBuilder = new StringBuilder(2);
  7. int result = ToAscii((uint)key, 0, GetKeyState(modifiers),
  8. outputBuilder, 0);
  9. if (result == 1)
  10. return outputBuilder[0];
  11. else
  12. throw new Exception("Invalid key");
  13. }
  14.  
  15. private const byte HighBit = 0x80;
  16. private static byte[] GetKeyState(Keys modifiers)
  17. {
  18. var keyState = new byte[256];
  19. foreach (Keys key in Enum.GetValues(typeof(Keys)))
  20. {
  21. if ((modifiers & key) == key)
  22. {
  23. keyState[(int)key] = HighBit;
  24. }
  25. }
  26. return keyState;
  27. }
  28.  
  29. [DllImport("user32.dll")]
  30. private static extern int ToAscii(uint uVirtKey, uint uScanCode,
  31. byte[] lpKeyState,
  32. [Out] StringBuilder lpChar,
  33. uint uFlags);
  34. }
  35.  
  36. char c = User32Interop.ToAscii(Keys.OemQuestion, Keys.ShiftKey); // = '?'
Add Comment
Please, Sign In to add comment