Click here to Skip to main content
15,918,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 ??
Posted
Updated 15-Apr-10 23:35pm
v2

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:

C#
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);
        }
    }
}
 
Share this answer
 
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 replaced e.KeyCode with e.Key and Keys.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);
                }
            }
        }

    }
}
 
Share this answer
 
v2
Right, I didn't think of that. So what about making a specific TextBox.Select function for each arrow key? Such as:

SQL
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);
}


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.
 
Share this answer
 
v3
I would keep the code very similar to what is given in Answer 1 but with two changes.

1) I would run it on the KeyUp event of the textbox (instead of KeyDown).
2) I would add textBox1.SelectionStart = textBox1.Text.Length + 1;
to my Left If condition.

So the code would look like this ->


C#
private void textBox1_KeyUp(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)
            {
                textBox1.SelectionStart = textBox1.Text.Length + 1;
                insert = '.';
            }
            else
            {
                insert = '-';
            }
            int i = textBox.SelectionStart;

            textBox1.Text = textBox1.Text.Insert(i, insert.ToString()); textBox1.Select(i + 1, 0);
        }
    }
}
 
Share this answer
 
As a reply to Awnser 4: I think that putting it in KeyUp will mess it up. Simply because it will execute as soon as the user releases the key. But as with any key you can just keep it pressed. In the case of left and right keys it will first scroll a position and upon release put a single character.

With KeyDown it will behave exactly the same as keeping any key pressed.
 
Share this answer
 
OH my! its works you saved me Abhinav cause I started to learn how to remap the keyboard keys low level ah!. Now I can move to the next step :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900