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. ? The Key Events

? The Key Events

Scheduled Pinned Locked Moved C#
helpquestion
2 Posts 1 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.
  • F Offline
    F Offline
    Frank Olorin Rizzi
    wrote on last edited by
    #1

    [mode="frustrated"] ARGH ! [/mode] Please, please, if you can help with this, *please* do so: I'm about to loose my mind on it :-) Here's the problem: I'm developing a simple WinForm application. Among other things, I'd like to set a private boolean flag, called "shiftPressed" to true when the user is pressing the Shift key. This flag is checked when doing other things (such as clicking on the form) to modify the behavior of the application as needed. So, I figured the logical thing to do was to intercept the KeyDown and the KeyUp events. In the Key down event, I would set the flag to true if the Shift key is pressed; in the KeyUp event I would unset the flag as needed. However, it seems that if I catch the KeyDown, the KeyUp event is not fired, and/or vice-versa (and let's not even talk about the KeyPressed event !). In short, I have private void myApp_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { MessageBox.Show("KeyDown"); e.Handled=false; } private void myApp_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e) { MessageBox.Show("KeyUp"); } and I see only "KeyDown" message boxes. Note that the line e.handled=false; can be there or not, but the behavior seems to be always the same. What can I do? Do I have to dive into public class TestMessageFilter: IMessageFilter {...} and public bool HandleKeys(Keys keyCode) { bool ret = true; switch(keyCode) { case Keys.Shift: ...do stuff... default: ret = false; break; } return ret; } ??? Thanks in advance, F.O.R.

    F 1 Reply Last reply
    0
    • F Frank Olorin Rizzi

      [mode="frustrated"] ARGH ! [/mode] Please, please, if you can help with this, *please* do so: I'm about to loose my mind on it :-) Here's the problem: I'm developing a simple WinForm application. Among other things, I'd like to set a private boolean flag, called "shiftPressed" to true when the user is pressing the Shift key. This flag is checked when doing other things (such as clicking on the form) to modify the behavior of the application as needed. So, I figured the logical thing to do was to intercept the KeyDown and the KeyUp events. In the Key down event, I would set the flag to true if the Shift key is pressed; in the KeyUp event I would unset the flag as needed. However, it seems that if I catch the KeyDown, the KeyUp event is not fired, and/or vice-versa (and let's not even talk about the KeyPressed event !). In short, I have private void myApp_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { MessageBox.Show("KeyDown"); e.Handled=false; } private void myApp_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e) { MessageBox.Show("KeyUp"); } and I see only "KeyDown" message boxes. Note that the line e.handled=false; can be there or not, but the behavior seems to be always the same. What can I do? Do I have to dive into public class TestMessageFilter: IMessageFilter {...} and public bool HandleKeys(Keys keyCode) { bool ret = true; switch(keyCode) { case Keys.Shift: ...do stuff... default: ret = false; break; } return ret; } ??? Thanks in advance, F.O.R.

      F Offline
      F Offline
      Frank Olorin Rizzi
      wrote on last edited by
      #2

      Go it ! Phew* ! I was seeing only KeyDown message boxes because of timing issues; before that, I was doing a mistake in the code of the KeyUp handler and the shiftPressed was being set to true when I was de-pressing the key. For anyone else who needs this, this seems to work: private void myApp_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if(e.Shift) shiftPressed=true; e.Handled=false; } private void myApp_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e) { shiftPressed=e.Shift; } We now return you to the universe next door. F.O.R.

      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