CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2008
    Posts
    39

    Keyboard filtering

    Hello,

    In my MFC application I want a keyboard filter so that on some situations only expected keys are accepted.

    Example:

    An edit control accepting only binary digits (0 or 1), all other keys beeing rejected.

    I think OnKeyDown has to be customized but I am not sure

    Thank you in helping me.

    Whitebird001

  2. #2
    Join Date
    May 2006
    Location
    beyond the pillars of hercules
    Posts
    295

    Re: Keyboard filtering

    have a look at this
    Keyboard Hook

  3. #3
    Join Date
    Nov 2008
    Posts
    13

    Re: Keyboard filtering

    Try subclassing the edit control and handle OnSysKeyDown().

    Regards,
    John
    http://www.simpletools.co.in

  4. #4
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: Keyboard filtering

    Quote Originally Posted by Cpp_Noob View Post
    Keyboard[/url]
    Unnecessary, overkill.
    Quote Originally Posted by unnamed View Post
    Try subclassing the edit control
    Subclassing is good.
    Details:
    Derive class using CEdit as a base class. Subclass control using Wizard. Add member variable using derived class for a variable type "control" (not a string). This will insert DDX calls necessary to subclass control.

    Quote Originally Posted by unnamed View Post
    handle OnSysKeyDown().

    Insert handler for WM_CHAR message in CEdit derived class. In the handler, filter characters you need by returning from the handler without calling a base class. You can also consider beep to let user know that wrong character was entered.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  5. #5
    Join Date
    Nov 2008
    Posts
    13

    Re: Keyboard filtering

    Quote Originally Posted by JohnCz View Post

    Insert handler for WM_CHAR message in CEdit derived class. In the handler, filter characters you need by returning from the handler without calling a base class. You can also consider beep to let user know that wrong character was entered.
    Sorry, I had OnKeyDown in my mind.

  6. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: Keyboard filtering

    Quote Originally Posted by whitebird001 View Post
    Example:

    An edit control accepting only binary digits (0 or 1), all other keys beeing rejected.

    I think OnKeyDown has to be customized but I am not sure
    The task (as it exactly is) is solved by ES_NUMBER edit control style.
    Best regards,
    Igor

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Keyboard filtering

    Quote Originally Posted by Igor Vartanov View Post
    The task (as it exactly is) is solved by ES_NUMBER edit control style.
    How?
    First it allows also digits from 2 to 9 to be entered.
    Second (from this link):
    ES_NUMBER
    Allows only digits to be entered into the edit control. Note that, even with this set, it is still possible to paste non-digits into the edit control.
    To change this style after the control has been created, use SetWindowLong.
    Victor Nijegorodov

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: Keyboard filtering

    Sorry, you're right, I have missed the point of 0/1. But regarding possible paste, the requirement to filter edit box text changes is different from filtering key presses.
    Best regards,
    Igor

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Keyboard filtering

    Quote Originally Posted by Igor Vartanov View Post
    ... But regarding possible paste, the requirement to filter edit box text changes is different from filtering key presses.
    Yes, of course!.
    And I am sorry too, this part (about pasting problem) was not for you but for OP!
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured