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 / C++ / MFC
  4. How to capture "[" and "]" keys on OnKeyDown()?

How to capture "[" and "]" keys on OnKeyDown()?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestionhelp
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.
  • T Offline
    T Offline
    TooShy2Talk
    wrote on last edited by
    #1

    Hi, Hope somebody can help me with this one. I want to capture pressing "[" and "]" on OnKeyDown() event. For controls like shift if(VK_SHIFT) For capturing letters, I use example pressing s. if(nChar=="s") but for other keys like "[", "]" and numerics it's not applicable. How can i capture other keys?

    R 1 Reply Last reply
    0
    • T TooShy2Talk

      Hi, Hope somebody can help me with this one. I want to capture pressing "[" and "]" on OnKeyDown() event. For controls like shift if(VK_SHIFT) For capturing letters, I use example pressing s. if(nChar=="s") but for other keys like "[", "]" and numerics it's not applicable. How can i capture other keys?

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

      TooShy2Talk wrote:

      For capturing letters, I use example pressing s. if(nChar=="s")

      you mean if (nChar == 'S') but you should always use Virtual Key codes, because nChar is the virtual key code, capital letter is identified by the Shift/caps key status. you may refer the list of key codes [Virtual-Key Codes ^] to use in your code.

      TooShy2Talk wrote:

      but for other keys like "[", "]" and numerics

      for numbers you may use '0'(30) - '9'(39) or VK_NUMPAD(0 - 9) because number can be entered in two different keys num pad and other above characters. "[" can still be identified by VK_OEM_4 by Virtual-Key Codes but depends on keyboard layout. so it is better to use [VkKeyScan(Ex)^] to convert character to virtual key code and compare,

      SHORT iSqrBracketOpen = VkKeyScan(_T('[')); // before hand once
      void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
      {
      if (nChar == iSqrBracketOpen)
      {
      MessageBox(_T("Open Square Bracket"));
      }
      ...
      }

      or use [ToAscii(Ex)^] or ToUnicode(Ex) to convert virtual key code to character and compare.

      T 1 Reply Last reply
      0
      • R Rajkumar R

        TooShy2Talk wrote:

        For capturing letters, I use example pressing s. if(nChar=="s")

        you mean if (nChar == 'S') but you should always use Virtual Key codes, because nChar is the virtual key code, capital letter is identified by the Shift/caps key status. you may refer the list of key codes [Virtual-Key Codes ^] to use in your code.

        TooShy2Talk wrote:

        but for other keys like "[", "]" and numerics

        for numbers you may use '0'(30) - '9'(39) or VK_NUMPAD(0 - 9) because number can be entered in two different keys num pad and other above characters. "[" can still be identified by VK_OEM_4 by Virtual-Key Codes but depends on keyboard layout. so it is better to use [VkKeyScan(Ex)^] to convert character to virtual key code and compare,

        SHORT iSqrBracketOpen = VkKeyScan(_T('[')); // before hand once
        void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
        {
        if (nChar == iSqrBracketOpen)
        {
        MessageBox(_T("Open Square Bracket"));
        }
        ...
        }

        or use [ToAscii(Ex)^] or ToUnicode(Ex) to convert virtual key code to character and compare.

        T Offline
        T Offline
        TooShy2Talk
        wrote on last edited by
        #3

        Thanks for the reply. It's a big help

        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