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

  • SEARCH
  • Home
  • 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 7898579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:28:16+00:00 2026-06-03T08:28:16+00:00

I am looking to find a generic way to support keyboard wedge scanning for

  • 0

I am looking to find a generic way to support keyboard wedge scanning for my WPF TextBox controls.

(I am really a novice when it comes to more advanced WPF features, so I would like to ask if I am going in the right direction before I put a lot of time into research.)

What I am wanting to do is to add an Attached Property (or something) to my TextBoxes that will cause it to read all input into the box and then call a custom “ScanCompleted” command with the scanned input.

If an Attached Property is not a good fit for this, then is there a way to get this command on a TextBox without descending my own custom “ScanableTextBox”?

(Note: The criteria for a scan (instead of typed data) is that it will start with the Pause key (#19) and end with a Return key (#13).)

  • 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-06-03T08:28:19+00:00Added an answer on June 3, 2026 at 8:28 am

    I think this could probably be accomplished with attached properties (behaviors), but would be much simpler and more straightforward to simply subclass TextBox and override the OnTextChanged, OnKeyDown, OnKeyUp and similar methods to add custom functionality.

    Why don’t you want to create your own control in this way?

    update: Attached Behaviour

    If you really don’t want a derived control, here is an attached behaviour that accomplishes this (explanation below):

      public class ScanReading
      {
        private static readonly IDictionary<TextBox, ScanInfo> TrackedTextBoxes = new Dictionary<TextBox, ScanInfo>();
    
        public static readonly DependencyProperty ScanCompletedCommandProperty =
          DependencyProperty.RegisterAttached("ScanCompletedCommand", typeof (ICommand), typeof (ScanReading), 
          new PropertyMetadata(default(ICommand), OnScanCompletedCommandChanged));
    
        public static void SetScanCompletedCommand(TextBox textBox, ICommand value)
        {
          textBox.SetValue(ScanCompletedCommandProperty, value);
        }
    
        public static ICommand GetScanCompletedCommand(TextBox textBox)
        {
          return (ICommand) textBox.GetValue(ScanCompletedCommandProperty);
        }
    
        private static void OnScanCompletedCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
          var textBox = d as TextBox;
          if (textBox == null)
            return;
    
          var command = (ICommand) e.NewValue;
    
          if (command == null)
          {
            textBox.Unloaded -= OnTextBoxUnloaded;
            textBox.KeyUp -= OnTextBoxKeyUp;
            TrackedTextBoxes.Remove(textBox);
          }
          else
          {
            textBox.Unloaded += OnTextBoxUnloaded;
            TrackedTextBoxes.Add(textBox, new ScanInfo(command));
    
            textBox.KeyUp += OnTextBoxKeyUp;
          }
        }
    
        static void OnTextBoxKeyUp(object sender, KeyEventArgs e)
        {
          var textBox = (TextBox) sender;
    
          var scanInfo = TrackedTextBoxes[textBox];
          if (scanInfo.IsTracking)
          {
            if (e.Key == Key.Return)
            {
              scanInfo.ScanCompletedCommand.Execute(textBox.Text);
              scanInfo.IsTracking = false;
            }
          }
          else if (string.IsNullOrEmpty(textBox.Text) && e.Key == Key.Pause)
          {
            TrackedTextBoxes[textBox].IsTracking = true;
          }
        }
    
        static void OnTextBoxUnloaded(object sender, RoutedEventArgs e)
        {
          var textBox = (TextBox) sender;
          textBox.KeyUp -= OnTextBoxKeyUp;
          textBox.Unloaded -= OnTextBoxUnloaded;
          TrackedTextBoxes.Remove(textBox);
        }
      }
    
      public class ScanInfo
      {
        public ScanInfo(ICommand scanCompletedCommand)
        {
          ScanCompletedCommand = scanCompletedCommand;
        }
    
        public bool IsTracking { get; set; }
        public ICommand ScanCompletedCommand { get; private set; }
      }
    

    Consume this by declaring a TextBox like so (where local is the namespace of your attached property, and ScanCompleted is an ICommand on your view-model):

    <TextBox local:ScanReading.ScanCompletedCommand="{Binding ScanCompleted}" />
    

    Now when this property is set, we add the TextBox to a static collection along with its associated ICommand.

    Each time a key is pressed, we check whether it is the Pause key. If it is, and if the TextBox is empty, we set a flag to true to start looking for the Enter key.

    Now each time a key is pressed, we check whether it is the Enter key. If it is, we execute the command, passing in the TextBox.Text value, and reset the flag to false for that TextBox.

    We’ve also added a handler for the TextBox.Unloaded event to clean up our event subscriptions and remove the TextBox from the static list.

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

Sidebar

Related Questions

I am looking to find a way to programatically (C++) control/secure access to the
I'm looking to find a way to round up to the nearest 500.I've been
Is there a way to find the declared generic argument type of an instance
Being compelled by the advantages I'm looking for a way to integrate generic programming
Looking for generic way how to sort a random list of files by its
I'm starting with WPF but I can't find the way to inherit from a
I have solved the more generic N Queens problem, but now I am looking
I'm looking to find information on how to update my XCode program that has
I am looking to find the slope between two vectors via linear regression in
I'm looking to find the add-on which will draw a tooltip window at the

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.