如何使箭头键充当另一个键? [英] How to make the arrows keys act as another keys?
问题描述
我是Silverlight的新手,并且对C#有简单的经验.现在我正在从事Silverlight项目.
我的问题是:在Silverlight中,如何对键盘按键进行编码以充当键盘上的其他按键,特别是如何使左箭头像. (点),当用户在文本框内的焦点上按下左箭头时,它将键入.以及以相同的方式使右箭头像-(破折号)
我最后得到的代码如下:
I''m new to Silverlight and have simple experiance with C#. Now I''m working on Silverlight project.
My question is: In Silverlight, How to code keyboard keys to act as other keys from the keyboard, specificly how to make the left arrow act like . (dot), when a user press the left arrow while the focus in a textbox it will type . and also in the same way how to make the right arrow act like - ( dash)
Last code i got to is the following:
string s = string.Empty;
public TextBoxKeyPress()
{
InitializeComponent();
txtInput.KeyUp += new KeyEventHandler(txtInput_KeyUp);
txtInput.KeyDown += new KeyEventHandler(txtInput_KeyDown);
}
void txtInput_KeyDown(object sender, KeyEventArgs e)
{
s = "";
switch (e.Key)
{
case Key.Left:
{
s = ".";
e.Handled = true;
break;
}
case Key.Right:
{
s = "-";
e.Handled = true;
break;
}
}
}
void txtInput_KeyUp(object sender, KeyEventArgs e)
{
TextBox txb = (TextBox)sender;
if (Keyboard.Modifiers == ModifierKeys.Shift) return;
if (Keyboard.Modifiers == ModifierKeys.Control) return;
if (txb.SelectedText.Length > 0)
txb.SelectedText = "";
switch (e.Key)
{
case Key.Left:
{
s = ".";
e.Handled = true;
break;
}
case Key.Right:
{
s = "-";
e.Handled = true;
break;
}
}
int i = txb.SelectionStart;
txb.Text = txb.Text.Insert(i, s);
txb.Select(i + 1, 0);
e.Handled = true;
}
但是问题是当我想在文本框中写..时.实际上,我最终写完了..-,因为箭头确实向左/向右移动了一步,然后写了.或-两者都做!
But the problem is when I want to write in the textbox .-. actually I end up writing ..- because the arrows do move left/right one step and then write . or - , they do the both! how to disable the basic arrows functions ??
推荐答案
嘿,
因此,目前我还不太了解为什么您决定同时拥有KeyUp和KeyDown,因为我认为当您在KeyDown中处理"它时,KeyUp甚至不应该出现.尽管我认为您的目标是正确的,但我还是为您粉碎了一些代码(这确实对我有用)
我也无法真正弄清楚为什么要将发件人投射到TextBox(我想出的唯一原因是您想在多个文本框上使用该机制).话虽如此,如果您想将发件人转换为特定对象,最好首先检查它是否实际上是该对象,否则您的代码将崩溃.
这是我为您提供的KeyDown函数:
Hey there,
So at the moment I don''t really understand why you decided to have both KeyUp and KeyDown as i think KeyUp should not even come up as you''ve "handled" with it in KeyDown. Though i think you are aiming in a right direction I''ve smashed some code together for you (That did work for me)
I also couldn''t really figure out why you are casting sender to a TextBox (the only reason i could come up with is that you''d like to use the mechanism on multiple textboxes). With that being said, if you ever want to cast sender to a specific object it''s best to first check if it''s actually that object otherwise you''re code will crash.
Here''s my KeyDown function for you:
private void txtInput_KeyDown(object sender, KeyEventArgs e)
{
if (sender is TextBox)
{
TextBox textBox = (TextBox)sender;
if (e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)
{
e.Handled = true;
char insert;
if (e.KeyCode == Keys.Left)
{
insert = '.';
}
else
{
insert = '-';
}
int i = textBox.SelectionStart;
textBox.Text = textBox.Text.Insert(i, insert.ToString());
textBox.Select(i + 1, 0);
}
}
}
你好乔迪,
感谢你的回复.我不知道该如何回答您的答案,所以我将其放在此处.使用Keycode和Keys时,您的代码可以在窗口表单应用程序中很好地工作-没问题.但是我正在与Silverlight一起工作,因此我曾试图找出解决方案,但没有用.我更改了代码-我将e.KeyCode
替换为e.Key
和Keys.Right
with Key.Right
-,以便可以将其编译为Silverlight应用程序.但是不能用箭头键进行移动和书写两种功能. & -我不知道使用Keycode/Keys和Key/Key或其他原因会对程序产生巨大的影响吗?!
代码:
Hello Jordy,
Thank you for your reply. I don''t know how to reply to your answer so I''m putting it here. Your code works GREAT- no problems- with window form application as you used Keycode and Keys. But I''m working with Silverlight i tried to figure out the solution before but does not work. I changed your code- I replacede.KeyCode
withe.Key
andKeys.Right
with Key.Right
- so I can compile it as Silverlight app. but does not work the arrow keys still do the both functions moving and writting . & -. I don''t know is there HUGE different influence on the program between using Keycode/Keys and Key/Key or because of something else?!
The code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightApplication4
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
textBox1.KeyDown += new KeyEventHandler(textBox1_KeyDown);
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (sender is TextBox)
{
TextBox textBox = (TextBox)sender;
if (e.Key == Key.Left || e.Key == Key.Right)
{
e.Handled = true;
char insert;
if (e.Key == Key.Left)
{ insert = ''.''; }
else
{ insert = ''-''; }
int i = textBox.SelectionStart;
textBox.Text = textBox.Text.Insert(i, insert.ToString());
textBox.Select(i + 1, 0);
}
}
}
}
}
对,我没想到.那么,如何为每个箭头键创建特定的TextBox.Select函数呢?如:
Right, I didn''t think of that. So what about making a specific TextBox.Select function for each arrow key? Such as:
if (e.KeyCode == Keys.Left)
{
insert = '.';
int i = textbox.SelectionStart;
textbox.Select(i + 1, 0);
}
else
{
insert = '-';
int i = textbox.SelectionStart;
textbox.Select(i - 1, 0);
}
这样,它应该撤消"在文本框内移动的光标并放入字符中.我认为,这种现象主要是由浏览器强制进行更改而不是您自己的Windows应用程序引起的.
This way it should "undo" the cursor moving inside the textbox and put in the character. I think this behaviour is mostly caused by the browser forcing that change as opposed to your own windows applications.
这篇关于如何使箭头键充当另一个键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!