开发者

How to code a keyboard button to switch between 2 modes?

I'm doing a project. I'm not going to details but I will simplify my idea. I'm using Morse Code ( dot and dash) and I have 2 methods: convert_MorseToChar() and Convert_MorseTonum(). In the convert_MorseToChar() method there is a switch to compare the input from a user which will be Morse codes and mapping it to characters:

  private String convert_MorseToChar(ref string Ch) 
  {
    switch (Ch)
        {
        Case ".-":
            MorsetoChar = "a"
            break;
        Case "-...":
            MorsetoChar = "b"
            break;
        Case "-.-.":
            MorsetoChar = "c"
            break;
        Case "-..":
            MorsetoChar = "d"
            break;
        Case ".":
            MorsetoChar = "e"
            break;
        }
   }

and the other method Convert_MorseToNum(), ues the SAME combinations of Morse codes but mapping them to numbers:

  private String Convert_MorseToNum(ref string Ch) 
  {
    switch (Ch)
        {
        Case ".-":
            MorsetoChar = "1"
            break;
        Case "-...":
            MorsetoChar = "2"
            break;
        Case "-.-.":
            MorsetoChar = "3"
            break;
        Case "-..":
            MorsetoChar = "4"
            break;
        Case ".":
            MorsetoChar = "5"
            break;
        }
   }

Now the senario is: there are 2 Textbox, one the user will write Morse codes in it and the other is for the output. The user will write dot . and dash - from the keyboard and press Enter then the program will go to ONE of the 2 methods to convert the Morse codes. Now wha开发者_运维技巧t tells the program where to go to convert?

my question is: I want to create mode key to switch between 2 modes: MorseToChar and MorseToNum. I want the down arrow key to act like a mode. When a user presses the down arrow then it the program will be in MorseToChar mode, whenever the user input the program directly use the method convert_MorseToChar to convert to characters. When the user press the down arrow again, the program will switch to MorseToNum mode here when ever the user input as morsecode, the program will directly use the method Convert_MorseToNum() to convert to numbers. How can I do that?

Please excuse my English, English is not my native language :)


You can consume the KeyDown event. If the pressed key is the down arrow, you switch modes:

private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    // Determine whether the keystroke is the down arrow.
    if (e.KeyCode == Keys.Down)
    {
        charMode = !charMode;
    }
}

Of course you will have to define your charMode as a bool. And when calling your conversion method, you will check its value.

private String Convert_Morse(ref string Ch) 
{
    if (charMode) return convert_MorseToChar(Ch)
    else
    return convert_MorseToNum(Ch);
}


You could use a boolean that gets toggled each time the desired key is pressed, like so:

bool NumericMode = false; // accessible to the Morse code methods
private void ToggleMode()
{
    if(NumericMode)
        NumericMode = false;
    else
        NumericMode = true;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜