1-9,0 keys above the keyboard
A-Z keys are equal to the A-Z upper case ascii characters.
For those characters, just cast the enum as so:
Keys k = // some key
char ascii = (char)(int)k;
Heres the output from a simple program that proves it:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Input;
class Program
{
static void Main(string[] args)
{
foreach (object value in Enum.GetValues(typeof(Keys)))
{
string name = Enum.GetName(typeof(Keys), value);
Console.WriteLine(name + " " + value + "=" + (char)(int)value);
}
}
}
None None=
LButton LButton=?
RButton RButton=?
Cancel Cancel=?
MButton MButton=?
XButton1 XButton1=?
XButton2 XButton2=?
Back Back=
Tab Tab=
LineFeed LineFeed=
Clear Clear=♀
Return Return=
Return Return=
ShiftKey ShiftKey=?
ControlKey ControlKey=?
Menu Menu=?
Pause Pause=?
CapsLock CapsLock=?
CapsLock CapsLock=?
HanguelMode HanguelMode=§
HanguelMode HanguelMode=§
HanguelMode HanguelMode=§
JunjaMode JunjaMode=?
FinalMode FinalMode=↑
HanjaMode HanjaMode=↓
HanjaMode HanjaMode=↓
Escape Escape=←
IMEConvert IMEConvert=∟
IMENonconvert IMENonconvert=?
IMEAceept IMEAceept=▲
IMEAceept IMEAceept=▲
IMEModeChange IMEModeChange=▼
Space Space=
PageUp PageUp=!
PageUp PageUp=!
Next Next="
Next Next="
End End=#
Home Home=$
Left Left=%
Up Up=&
Right Right='
Down Down=(
Select Select=)
Print Print=*
Execute Execute=+
Snapshot Snapshot=,
Snapshot Snapshot=,
Insert Insert=-
Delete Delete=.
Help Help=/
D0 D0=0
D1 D1=1
D2 D2=2
D3 D3=3
D4 D4=4
D5 D5=5
D6 D6=6
D7 D7=7
D8 D8=8
D9 D9=9
A A=A
B B=B
C C=C
D D=D
E E=E
F F=F
G G=G
H H=H
I I=I
J J=J
K K=K
L L=L
M M=M
N N=N
O O=O
P P=P
Q Q=Q
R R=R
S S=S
T T=T
U U=U
V V=V
W W=W
X X=X
Y Y=Y
Z Z=Z
LWin LWin=[
RWin RWin=\
Apps Apps=]
Sleep Sleep=_
NumPad0 NumPad0=`
NumPad1 NumPad1=a
NumPad2 NumPad2=b
NumPad3 NumPad3=c
NumPad4 NumPad4=d
NumPad5 NumPad5=e
NumPad6 NumPad6=f
NumPad7 NumPad7=g
NumPad8 NumPad8=h
NumPad9 NumPad9=i
Multiply Multiply=j
Add Add=k
Separator Separator=l
Subtract Subtract=m
Decimal Decimal=n
Divide Divide=o
F1 F1=p
F2 F2=q
F3 F3=r
F4 F4=s
F5 F5=t
F6 F6=u
F7 F7=v
F8 F8=w
F9 F9=x
F10 F10=y
F11 F11=z
F12 F12={
F13 F13=|
F14 F14=}
F15 F15=~
F16 F16=?
F17 F17=?
F18 F18=?
F19 F19=?
F20 F20=?
F21 F21=?
F22 F22=?
F23 F23=?
F24 F24=?
NumLock NumLock=?
Scroll Scroll=?
LShiftKey LShiftKey=
RShiftKey RShiftKey=?
LControlKey LControlKey=¢
RControlKey RControlKey=£
LMenu LMenu=?
RMenu RMenu=¥
BrowserBack BrowserBack=▌
BrowserForward BrowserForward=§
BrowserRefresh BrowserRefresh="
BrowserStop BrowserStop=c
BrowserSearch BrowserSearch=a
BrowserFavorites BrowserFavorites=?
BrowserHome BrowserHome=?
VolumeMute VolumeMute=-
VolumeDown VolumeDown=r
VolumeUp VolumeUp=_
MediaNextTrack MediaNextTrack=°
MediaPreviousTrack MediaPreviousTrack=±
MediaStop MediaStop=2
MediaPlayPause MediaPlayPause=3
LaunchMail LaunchMail='
SelectMedia SelectMedia=μ
LaunchApplication1 LaunchApplication1=?
LaunchApplication2 LaunchApplication2=·
Oem1 Oem1=o
Oem1 Oem1=o
Oemplus Oemplus=
Oemcomma Oemcomma=?
OemMinus OemMinus=?
OemPeriod OemPeriod=_
Oem2 Oem2=?
Oem2 Oem2=?
Oem3 Oem3=A
Oem3 Oem3=A
Oem4 Oem4=U
Oem4 Oem4=U
OemPipe OemPipe=ü
OemPipe OemPipe=ü
Oem6 Oem6=Y
Oem6 Oem6=Y
OemQuotes OemQuotes=_
OemQuotes OemQuotes=_
Oem8 Oem8=?
Oem102 Oem102=a
Oem102 Oem102=a
ProcessKey ProcessKey=?
Packet Packet=?
Attn Attn=?
Crsel Crsel=÷
Exsel Exsel=o
EraseEof EraseEof=ù
Play Play=ú
Zoom Zoom=?
NoName NoName=ü
Pa1 Pa1=y
OemClear OemClear=_
KeyCode KeyCode=?
Shift Shift=
Control Control=
Alt Alt=
Modifiers Modifiers=
I actually checked that before posting(Keys, go to definition), but I need more keys than just a-z, 1-0, and I'd really prefer not to hard code it when it is supposed to be in windows already, people get confused if it defaults to a different keyboard layout than the one they are using.
I guess I can rewrite the entire keyboard layout driver/thing, but that seems like silly duplication somehow and there are many international characters not on my keyboard I don't know how to type.. It's annoying enough just switching between UK/US..
I think u don't understand the reply from Kris Nye
Each keyvalue from the enumerator presents an ascii code. So its all u need to present it as character. His code is not limited to 0-9, a-z cause when using ascii values, converting the key values to a character will do exactly the same as pressing the key in notepad.
As for keyboard layouts, i guess microsoft allready solved your problem ;)
Where i live we use azerty keyboards instead of qwerty and when i say in my code Keys.A ... then Keys.A really means A on my keyboard instead of Q like in most games and native directx sdk.
So i don't see any problems with the code of Kris Nye ...its perfect ... and i will also need it in few days ;)
Ok, if I alledgedly don't understand trivial code, and you do.. perhaps you can tell me where I can find codes like !"£$%^&*() on the keyboard along with non-english characters that use umlauts and accents.. Ok, SOME of the keys are ascii compatable, some are not.. just A-Z 1-0 and a couple of control codes. Anyway, since when were mouse buttons, shift and caps lock ascii ? For that matter what about lower case a-z and -=_+[]{};'#:@~,./<>?\|`? They don't get returned as ascii at all, meaning they need a custom handler. PageDown=34 ? Pfft.
For reference, I wrote a keyboard routine, found the keyboard problems, and then checked the values in Keys before posting, which is why I asked in the first place. All I wanted was a simple way to get the full character set from the keyboard (with the users layout) and not to have to hard code a custom layout handler that duplicates work already done if it's at all avoidable.
(Though I am pleased if it actually handles an azerty keyboard ok, thanks for that - although, does it affect things like default FPS controls if it maps like that ?)
Try this :
KeyboardState state = Keyboard.GetState ( );
char buffer;
byte[] currentState = state.GetType().GetField("pCurrentState", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(state) as byte[];
ToUnicode ( key, 0, currentState, out buffer, 1, 0 );
I know, this is *very* bad... We are trying to find something better.
I was really trying to avoid hard coding something like that, simply because I don't have a complete set of international keymaps - that code looks to support only the US keyboard and ignores international characters. Hmm, wonder if I could load the keyboard details into an array holding those keycodes, now, how to do that..
The keyboard search goes on. It sounds so simple..