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. Keyboard input handle problem

Keyboard input handle problem

Scheduled Pinned Locked Moved C#
helpquestion
3 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.
  • E Offline
    E Offline
    Emmet_Brown
    wrote on last edited by
    #1

    Hi all, I want to close the from with a keyboard input 'K' but I cannot handle the input. Before asking here, I've done lots of researches on web and especially on msdn. This is not the first time I've been using KeyDown event but now I'm on a new computer and I cannot figure out why this time it does not work. my code is simple:

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
    if (e.KeyCode == Keys.K)
    {
    this.Close();
    }
    }

    and on the designer side: this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown); All other events work just fine, like MouseOver, MouseDown or MouseMove but when it comes to KeyDown or KeyPress, it doesn't work any suggestions? :(

    R 1 Reply Last reply
    0
    • E Emmet_Brown

      Hi all, I want to close the from with a keyboard input 'K' but I cannot handle the input. Before asking here, I've done lots of researches on web and especially on msdn. This is not the first time I've been using KeyDown event but now I'm on a new computer and I cannot figure out why this time it does not work. my code is simple:

      private void Form1_KeyDown(object sender, KeyEventArgs e)
      {
      if (e.KeyCode == Keys.K)
      {
      this.Close();
      }
      }

      and on the designer side: this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown); All other events work just fine, like MouseOver, MouseDown or MouseMove but when it comes to KeyDown or KeyPress, it doesn't work any suggestions? :(

      R Offline
      R Offline
      RaviRanjanKr
      wrote on last edited by
      #2

      you need to enable KeyPreview of Form for which you want to write code as

      this.KeyPreview = true;

      and then write

      private void Form1_KeyDown(object sender, KeyEventArgs e)
      {
      if (e.KeyCode == Keys.K)
      {
      this.Close();
      }
      }

      or

      private void Form1_KeyPress(object sender, KeyPressEventArgs e)
      {
      if (e.KeyChar == 'k')
      {
      Close();
      }
      }

      Both Work fine

      E 1 Reply Last reply
      0
      • R RaviRanjanKr

        you need to enable KeyPreview of Form for which you want to write code as

        this.KeyPreview = true;

        and then write

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
        if (e.KeyCode == Keys.K)
        {
        this.Close();
        }
        }

        or

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
        if (e.KeyChar == 'k')
        {
        Close();
        }
        }

        Both Work fine

        E Offline
        E Offline
        Emmet_Brown
        wrote on last edited by
        #3

        It worked Thank you! :D

        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