none
Ctrl-Ins Shift-Ins

    Question

  • Is it possible to intercept/redefine Ctrl-Ins and Shift-Ins in a TextBox Control ?
    for example when I press Ctrl-C or Ctrl-V the KeyPress event is fired and is also possible to intercept Ctrl-C Ctrl-V via a Menu Strip

    Thank you
    Manuel
    Friday, September 18, 2009 7:58 AM

Answers

  • Add a new class to your project and paste the code shown below.  Compile.  Drop the new control from the top of the toolbox onto your form.  I had to guess at the meaning of the shortcut keys, much like your user will.

    using System;
    using System.Windows.Forms;

    class MyTextBox : TextBox {
      protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
        if (keyData == (Keys.Control | Keys.Insert)) {
          this.Paste();
          return true;
        }
        if (keyData == (Keys.Shift | Keys.Insert)) {
          this.Copy();
          return true;
        }
        return base.ProcessCmdKey(ref msg, keyData);
      }
    }

    Hans Passant.
    • Marked as answer by Manuel Marini Monday, September 21, 2009 8:25 AM
    Saturday, September 19, 2009 7:23 PM
    Moderator

All replies

  • Well... It seems that can intercept Ctrl-Ins or Shift-Ins only using a MenuStrip or ContextMenuStrip...
    Friday, September 18, 2009 8:08 AM
  • Add a new class to your project and paste the code shown below.  Compile.  Drop the new control from the top of the toolbox onto your form.  I had to guess at the meaning of the shortcut keys, much like your user will.

    using System;
    using System.Windows.Forms;

    class MyTextBox : TextBox {
      protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
        if (keyData == (Keys.Control | Keys.Insert)) {
          this.Paste();
          return true;
        }
        if (keyData == (Keys.Shift | Keys.Insert)) {
          this.Copy();
          return true;
        }
        return base.ProcessCmdKey(ref msg, keyData);
      }
    }

    Hans Passant.
    • Marked as answer by Manuel Marini Monday, September 21, 2009 8:25 AM
    Saturday, September 19, 2009 7:23 PM
    Moderator
  • Hi Manuel Marini,

     

    Could you please tell me why you want to make these two shortcut keys? Shift + Insert key has been defined by the system. If you replace it with your own logical, will it affect user experience?

     

    Anyway, I think nobugz’s sample code is suitable for your requirement. Override ProcessCmdKey can intercept user input in your .NET application. Since system doesn’t define “Control+Insert”, you can easily handle other event such as the KeyDown event to intercept "Control+Insert" and code your own logical.

     

    If I misunderstood you, please feel free to tell me.

     

    Sincerely,

    Kira Qian

    Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    Monday, September 21, 2009 6:28 AM
  • Hi,

    The content of the text box has to be sent to a very old communication system (telex) this system doesn't support some characters. I need to filter these characters directly when the user types them and I need also to filter the paste operation (Shift-Ins and Ctrl-V) to have WYSIWYG editing.
    For example è has to become E', $ has to become USD.

    I was looking for a solution with some kind of subclassing and overriding the ProcessCmdKey is perfect for my needs.

    Thank you very much Kira Qian and Hans Passant.


    Manuel Marini

    Monday, September 21, 2009 8:53 AM