在 Xamarin.Android 中将键码转换为字符
Convert Keycode to char in Xamarin.Android
在我的自定义视图中按下某个键时,将调用 OnKeyPress:
public DrawView(Context context, IAttributeSet attributeSet) : base(context, attributeSet)
{
KeyPress += OnKeyPress;
}
private void OnKeyPress(object sender, KeyEventArgs e)
{
Keycode code = e.KeyCode;
// How to convert to char?
}
如何将键码转换为字符?例如:
"Space" -> " "
"Enter" -> "\n"
"A" -> "A"
尝试这样的事情:
public override bool OnKeyUp (Keycode keyCode, KeyEvent e)
{
char keyPressed = (char) e.GetUnicodeChar();
Log.Debug (string.Format ("On KeyUp is:{0}", keyPressed));
return base.OnKeyUp (keyCode, e);
}
根据 Vaikesh 的回答,这是解决方案:
private void OnKeyPress(object sender, KeyEventArgs e)
{
char c = Convert.ToChar(e.UnicodeChar);
}
在我的自定义视图中按下某个键时,将调用 OnKeyPress:
public DrawView(Context context, IAttributeSet attributeSet) : base(context, attributeSet)
{
KeyPress += OnKeyPress;
}
private void OnKeyPress(object sender, KeyEventArgs e)
{
Keycode code = e.KeyCode;
// How to convert to char?
}
如何将键码转换为字符?例如:
"Space" -> " "
"Enter" -> "\n"
"A" -> "A"
尝试这样的事情:
public override bool OnKeyUp (Keycode keyCode, KeyEvent e)
{
char keyPressed = (char) e.GetUnicodeChar();
Log.Debug (string.Format ("On KeyUp is:{0}", keyPressed));
return base.OnKeyUp (keyCode, e);
}
根据 Vaikesh 的回答,这是解决方案:
private void OnKeyPress(object sender, KeyEventArgs e)
{
char c = Convert.ToChar(e.UnicodeChar);
}