Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 388397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:45:56+00:00 2026-05-12T15:45:56+00:00

Related (but not a dupe!) to this question: Help with the WPF TextCompositionManager events

  • 0

Related (but not a dupe!) to this question: Help with the WPF TextCompositionManager events

When using the TextCompositionManager I’m having an issue where, if an input control such as the TextBox has focus, the TextBox will “steal” the space character before I can get a chance to act on it.

For example, we have the following code:

public Window1()
{
  TextCompositionManager.AddPreviewTextInputStartHandler
    (this, PreviewTextInputStartHandler);
}

private void PreviewTextInputHandler(object sender, TextCompositionEventArgs e)
{
  CaptureTextBlock.Text += e.Text;
  e.Handled = true;
}

where Window1 looks like:

<!--standard window crap above here-->
<StackPanel>
  <TextBlock Name="CaptureTextBlock" />
  <TextBox Name="ThievingBastard" />
</StackPanel>
<!-- snip -->

Now, if I run this application and immediately type “I haet thieving bastards”, the TextBlock will contain the text “I haet thieving bastards” and the textbox will be empty.

If, however, I focus the textbox (i.e., textbox has keyboard focus), after typing the above line the textblock will contain the text “Ihaetthievingbastards” and the textbox will contain the text ” ” (3 spaces).


I have two questions:
1) Can I prevent this from happening with just the facilities provided by the TextCompositionManager?
2) If not, where the hell do I plug into the text input stack so that I can fully control text input within my WPF application (negative points for even thinking about p/invoking) (you just thought about it, negative points added)?


Update

I’m using a hacky workaround where I handle the tunneling KeyDown event from the InputManager for spaces only. This method is very awkward, inefficient and basically stinks. Still looking for a better way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T15:45:56+00:00Added an answer on May 12, 2026 at 3:45 pm

    For the benefit of others, my hacky code.

    My particular app is waiting for a card swipe from a card reader. The following lives in the constructor of the object that watches for a card swipe (this is a side project; most comment cursing edited out):

    // this is where we handle the space and other keys wpf f*s up.
    System.Windows.Input.InputManager.Current.PreNotifyInput += 
        new NotifyInputEventHandler(PreNotifyInput);
    // This is where we handle all the rest of the keys
    TextCompositionManager.AddPreviewTextInputStartHandler(
        Application.Current.MainWindow, 
        PreviewTextInputHandler);
    

    The two methods:

    /// <summary>
    /// Handles the PreNotifyInput event of the input manager.
    /// </summary>
    /// <remarks>Because some controls steal away space (and other) characters, 
    /// we need to intercept the space and record it when capturing.</remarks>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The 
    /// <see cref="System.Windows.Input.NotifyInputEventArgs"/> 
    /// instance containing the event data.</param>
    private void PreNotifyInput(object sender, NotifyInputEventArgs e)
    {
        // I'm only interested in key down events
        if (e.StagingItem.Input.RoutedEvent != Keyboard.KeyDownEvent)
            return;
        var args = e.StagingItem.Input as KeyEventArgs;
        // I only care about the space key being pressed
        // you might have to check for other characters
        if (args == null || args.Key != Key.Space)
            return;
        // stop event processing here
        args.Handled = true;
        // this is my internal method for handling a keystroke
        HanleKeystroke(" ");
    }
    
    
    /// <summary>
    /// This method passes the event to the HandleKeystroke event and turns
    /// off tunneling depending on whether or not Capturing is true.
    /// Also calls StopCapturing when appropriate.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The 
    /// <see cref="System.Windows.Input.TextCompositionEventArgs"/> 
    /// instance containing the event data.</param>
    private void PreviewTextInputHandler(object sender, 
        TextCompositionEventArgs e)
    {
        HanleKeystroke(e.Text);
    }
    

    When somebody presses a key (or a keystroke is sent to the system) the PreNotifyInput event fires. In this case, I determine if it is a special key (for me I have to worry about the space, but other keys apparently need special attention). If it is a special key, I “handle” the event, stopping all further processing of this keystroke. I then call my internal processing method passing in the space (or whatever special key I just intercepted).

    All other keys are handled by the PreviewTextInputHandler method.

    This code has a lot of stuff stripped out of it. Determining when a swipe event has happened, determining when the swipe has completed, safeguards (timeouts in case I never stop capturing a swipe), etc is removed. How you do this stuff will depend on your code requirements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

[This question is related to but not the same as this one .] My
[This question is related to but not the same as this one .] If
This question is related to (but perhaps not quite the same as): Does Django
This is kind of related to my previous question, but not really. I have
This question might not seem programming related at first, but let me explain. I'm
This is not exactly a programming question, but it's highly related. We are writing
I´m not sure if this is really a programming question, but it's related to
This is not yet particularly programing related but, I am very interested in how
It's not quite a programming question, but I hope its related closely enough. Do
It may seem as if this is question is a dupe, but please bear

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.