2
Answers

Defining behavior for Up keyPress

peter

peter

14y
3.1k
1


I have a Windows forms based Telnet client where I want to define the behaviour of certain keystrokes in the RichTextBox holding user data to send.
You can see from the code below that the 'enter/return' key will send the RichTextBox contents + a newline character '\n' and the 'escape' key will clear the RichTextBox. Both work fine. I cannot find any which way to define the behaviour of the 'Up' key. The RichTextBox is not multiline and is not word wrap so it is not as though there needs to be a default behaviour for the 'Up' key. At the moment, for testing purpose, I am trying to define the 'Up' key to simply display a message box.

private void rtbInput_KeyPress(object sender, KeyPressEventArgs e)
{
   switch (e.KeyChar)
   {
      case (char)Keys.Return :
         if (TELNET_CLIENT != null && TELNET_CLIENT.Connected)
         {
            string TEXT_TO_SEND = rtbInput.Text.Trim();
            if (!string.IsNullOrEmpty(TEXT_TO_SEND))
            {
               UpdateHistory(TEXT_TO_SEND);
               bgwMain.RunWorkerAsync(TEXT_TO_SEND +
Environment.NewLine);
               byte[] BYTES_TO_SEND = Encoding.ASCII.GetBytes(TEXT_TO_SEND + "\n");
               TELNET_CLIENT.BeginSend(BYTES_TO_SEND, 0, BYTES_TO_SEND.Length,
SocketFlags.None, new AsyncCallback(SocketSend), TELNET_CLIENT);
            }
         }
         rtbInput.Clear();
         break;
      case (char)Keys.Escape :
         rtbInput.Clear();
         break;
      case (char)Keys.Up :
         MessageBox.Show("hello");
         break;
   }
}
 
Answers (2)
0
peter

peter

NA 6 0 14y
Worked a treat. Thanks very much.
0
Jaish Mathews

Jaish Mathews

NA 7.3k 1.2m 14y

 
Peter,
That won't fire in Key_Press. I done your code in Key_Down and now it's firing.
private
void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Return:
/* if (TELNET_CLIENT != null && TELNET_CLIENT.Connected)
{
string TEXT_TO_SEND = rtbInput.Text.Trim();
if (!string.IsNullOrEmpty(TEXT_TO_SEND))
{
UpdateHistory(TEXT_TO_SEND);
bgwMain.RunWorkerAsync(TEXT_TO_SEND + Environment.NewLine);
byte[] BYTES_TO_SEND = Encoding.ASCII.GetBytes(TEXT_TO_SEND + "\n");
TELNET_CLIENT.BeginSend(BYTES_TO_SEND, 0, BYTES_TO_SEND.Length, SocketFlags.None, new AsyncCallback(SocketSend), TELNET_CLIENT);
}
}
rtbInput.Clear();*/

MessageBox.Show("Pressed Enter");
break;
case Keys.Escape:
//rtbInput.Clear();
MessageBox.Show("Pressed Escape");
break;
case Keys.Up:
MessageBox.Show("Pressed Up Arrow");
break;
}

}