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
  1. Home
  2. General Programming
  3. C#
  4. Control Key Combo

Control Key Combo

Scheduled Pinned Locked Moved C#
csharpgame-devhelptutorial
2 Posts 2 Posters 0 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.
  • D Offline
    D Offline
    deanoA
    wrote on last edited by
    #1

    Hi! Im creating a game using C#. Now I need help on how to read combo keys. Like if the user presses ctrl-x, It would then exit the game. Please help. "To teach is to learn twice"

    H 1 Reply Last reply
    0
    • D deanoA

      Hi! Im creating a game using C#. Now I need help on how to read combo keys. Like if the user presses ctrl-x, It would then exit the game. Please help. "To teach is to learn twice"

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Simply handle any of the "key" events on your controls or your Form. I recommend the Form, and then set the Form.KeyPreview property to true so that the form gets a change to handle the key sequences before the controls do, giving you a single point of "key" event handling. Now, modifiers like Shift and Ctrl don't raise events like Control.KeyDown, but you can get them easily through the Control.ModifierKeys static property, or - depending on which event you use - from the KeyEventArgs that is passed to your event handler. So, change the KeyPreview event on your form to true. Create a new event handler for the KeyDown event (or the KeyUp or KeyPress events), and do something like the following:

      private void MyForm_KeyDown(object sender,
      KeyEventArgs e)
      {
      if (e.KeyCode == Keys.X && e.Control)
      {
      e.Handled = true; // Don't let child controls process this
      this.Close(); // Close your form or use Application.Exit()
      }
      }

      If you wanted to support user-defineable combos, just pass the KeyEventArgs or part of its data through some key map that wouldn't be hard to create (see the Keys enumeration, which might help).

      -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

      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