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. How can I see if LeftShift or RightShift was pressed?

How can I see if LeftShift or RightShift was pressed?

Scheduled Pinned Locked Moved C#
questioncsharphelp
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.
  • A Offline
    A Offline
    adfgh75
    wrote on last edited by
    #1

    I'm developing a C# application where I need to find out if the left shift key has been pressed down or the right shift key. I've tried to use the KeyDown event with the following code: private void KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if(e.Control && (e.KeyCode == Keys.D1) && (e.KeyCode == Keys.LShiftKey)) { //Do stuff here } } I can read the keypresses but I can not see if the left shift key is pressed, only that a shift key was pressed. The e.KeyCode == Keys.LShiftKey doesn't work since it seems that KeyCode don't collect info about which shift that was pressed. Any help appreciated! Thaks! Andrew

    T 1 Reply Last reply
    0
    • A adfgh75

      I'm developing a C# application where I need to find out if the left shift key has been pressed down or the right shift key. I've tried to use the KeyDown event with the following code: private void KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if(e.Control && (e.KeyCode == Keys.D1) && (e.KeyCode == Keys.LShiftKey)) { //Do stuff here } } I can read the keypresses but I can not see if the left shift key is pressed, only that a shift key was pressed. The e.KeyCode == Keys.LShiftKey doesn't work since it seems that KeyCode don't collect info about which shift that was pressed. Any help appreciated! Thaks! Andrew

      T Offline
      T Offline
      tarasn
      wrote on last edited by
      #2

      Declare the function somewhere inside your class: [DllImport("user32")] public static extern short GetKeyState (Keys VirtKey); On your KeyDown event, you can check which shift key is pressed in this way: bool lshiftpressed = ((GetKeyState(Keys.LShiftKey) & 256)==256); bool rshiftpressed = ((GetKeyState(Keys.RShiftKey) & 256)==256); DevIntelligence.com - My blog for .Net Developers

      A 1 Reply Last reply
      0
      • T tarasn

        Declare the function somewhere inside your class: [DllImport("user32")] public static extern short GetKeyState (Keys VirtKey); On your KeyDown event, you can check which shift key is pressed in this way: bool lshiftpressed = ((GetKeyState(Keys.LShiftKey) & 256)==256); bool rshiftpressed = ((GetKeyState(Keys.RShiftKey) & 256)==256); DevIntelligence.com - My blog for .Net Developers

        A Offline
        A Offline
        adfgh75
        wrote on last edited by
        #3

        It worked perfectly! Thanks! -A

        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