Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Handling keystrokes

Handling keystrokes

Scheduled Pinned Locked Moved C#
winformscsharpquestion
3 Posts 2 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jesarg
    wrote on last edited by
    #1

    I have a WinForms application whose main form contains a custom user control (and that custom user control, in turn, contains other custom user controls, etc). If I want the main form to handle keystrokes (such as Ctrl-S or Ctrl-X) without allowing the inner user controls to handle them first, is there any easy way I can guarantee that the main form will pick up the keystroke first (100% of the time)?

    L 1 Reply Last reply
    0
    • J jesarg

      I have a WinForms application whose main form contains a custom user control (and that custom user control, in turn, contains other custom user controls, etc). If I want the main form to handle keystrokes (such as Ctrl-S or Ctrl-X) without allowing the inner user controls to handle them first, is there any easy way I can guarantee that the main form will pick up the keystroke first (100% of the time)?

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, you will need the Form.KeyPreview property, the Form.KeyDown event, and the KeyEventArgs.Handled and KeyEventArgs.SuppressKeyPress properties. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      Sorry for any delays in replying, I currently don't always get e-mail notifications.


      J 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, you will need the Form.KeyPreview property, the Form.KeyDown event, and the KeyEventArgs.Handled and KeyEventArgs.SuppressKeyPress properties. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        Sorry for any delays in replying, I currently don't always get e-mail notifications.


        J Offline
        J Offline
        jesarg
        wrote on last edited by
        #3

        Thanks a lot. The topic of the intricacies of handling keystrokes is probably worth an article in itself. In my test application, I made a form with a listbox in it. The listbox contained several items, including an item that started with "S", and the application handled the Control-S functionality by displaying a messagebox. Although the Control-S messagebox still showed up when Control-S was pressed while the listbox had focus, the application also highlighted the listbox item that started with "S" (default functionality for a listbox is to highlight based on keypresses regardless of which modifiers are also pressed). The only way to suppress this functionality was to set the KeyEventArgs.SuppressKeyPress property of the Listbox.KeyDown event (it couldn't be done in the Form.KeyDown event). Thus, I can guarantee that my main form handles keypresses like I want it to, but I still have to hunt down the sub-controls one-by-one to make sure they don't handle keypresses when I don't want them to.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups