14 Replies Latest reply on Apr 19, 2011 12:33 PM by Flex harUI

    std barcode reader - keyboardevent for stream of chars - help :(

    Lifebelt

      Hi all,

       

      It's the very first time for me in adobe forum so.. hi everyone

      I've got a question related to my air application. This application has many controls like textinput button etc. etc.

      I would like to catch a barcode (a very fast keyboard stream of chars) in the main application (this isn't a problem I already can do this) and, if it is a barcode then i would like to stop the propagation of the chars otherwise the propagation of the chars continue as usual

       

      In order to catch the stream of chars I've used a timer, restarted every time i retrieve a character, if this timer expire and I have collect more the 5 chars I suppose the chars collected could be a barcode otherwise the chars (if they are less than 5) should be normal character from the user that use a keyboard

       

      the effect is:

      first case:

      a fast keyboard chars coming (a.. ok this is a barcode)

      stops the writing for example in the related textinput that has focus

      elaborate the barcode

      ...

       

      second case:

      a character or two is arrived from the keyboard (ok this isn't a barcode)

      continue with propagation and the related textinput show the character

      ...

       

      is this possible?

       

      I've tryed it.. but I can't find the solution, could anyone help me?

       

      thank you

       

      edit: if it isn't clear, please let me know, I can try to explain the problem in a better way

        • 1. Re: std barcode reader - keyboardevent for stream of chars - help :(
          Zolotoj Level 3

          You could configure your scanner to send a prefix - special character that your program would use to recognize barcode scanning.

          • 2. Re: std barcode reader - keyboardevent for stream of chars - help :(
            Lifebelt Level 1

            Thank you for your reply

            if it's possible I would like to evate this possibility.

             

            I don't know if is possible waiting some milliseconds or stop temporarly the propagation in the keyboardevent on the main application in order to collect the entire barcode and then make the decision of stopping the entire events propagation.

             

            something like:

             

            private function keyHandler(event:KeyboardEvent):void

            {

                 barcode += String.fromCharCode(event.charCode)

             

                

                 // wait here but continue to accept the keyboard input

             

                 if(barcode.length > 5)

                 {

                      // this is a barcode

                      event.stopPropagation();

                      event.preventDefault();

                 }

             

            }

            • 3. Re: std barcode reader - keyboardevent for stream of chars - help :(
              Zolotoj Level 3

              I might be possible to but I would not base my solution on some empirical data because on another computer/scanner it can be different dealys to set.

              Just configure your scanners to send a special character in a front of a bar code. It's called perfix or label. Or change your solution to use serial scanners. Then you are in the full control.

              • 4. Re: std barcode reader - keyboardevent for stream of chars - help :(
                Lifebelt Level 1

                probably you are right, but I have to use every type of barcode scanner that my customers already have (I don't know if their barcode reader has this prefix character configurable), the keyboard delay is the only way that I've found to provide this "compatibility".

                • 5. Re: std barcode reader - keyboardevent for stream of chars - help :(
                  Zolotoj Level 3

                  I would think that most of scanners being in use today should have a prefix configurable, but it can be difficult to control every scanner a client can get to use. I would go with a serial scanner solution if it's not too late. A price for serial scanners practically the same as for the keyboard's.

                  • 6. Re: std barcode reader - keyboardevent for stream of chars - help :(
                    Lifebelt Level 1

                    hehe I can't control what is the barcode reader (and I can't  force the customer to buy a new barcode scanner )

                     

                    this is why I'm trying to stop the propagation temporarly

                     

                    but thank you very much for your effor

                    • 7. Re: std barcode reader - keyboardevent for stream of chars - help :(
                      oldMster Level 3

                      Unfortunately, if you are trying to support 'unknown' barcode readers, not all barcode readers send the characters without intercharacter delays.  Some of them insert a delay between characters (on some readers this delay is programmable) so that the input doesn't happen any faster than a user typing.

                       

                      The other thing you should be aware of is that if there isn't an intercharacter delay built in to your reader, and it is programmed to send an 'enter' key at the end of the input, AND you have defined an 'enter' event handler for the text input, the 'enter' event will be triggered as soon as the 'enter' character is put into the input buffer, regardless of whether all the previous characters have been processed from the input buffer.  To handle this I had to change the 'enter' event handler to only trigger a timer event, and then 'tune' that timer so that the remaining characters were processed into the text input field before the real 'enter' processing was triggered by the timer.

                       

                      Mark

                      • 8. Re: std barcode reader - keyboardevent for stream of chars - help :(
                        Flex harUI Adobe Employee

                        Did you consider always stopping propagation of keyboard events?  Store each

                        keystroke in an array and if the sequence cannot be a barcode (because of a

                        timeout, number of characters received, or other criteria), then add the

                        characters to whatever component has focus.

                        • 9. Re: std barcode reader - keyboardevent for stream of chars - help :(
                          Lifebelt Level 1

                          Yes Sir,

                           

                          I've also made some test using the stoppropagation and preventdefault at every keyboardevent.

                          the problem is when the focus is inside a textinput and the character pressed on the keyboard is a back arrow, backspace canc or a selection, because in order to do that I should remap every key and move the cursor properly. this because of the keyboardevent are dispatched as a result of a keyboard input and not also like an input

                          We had made this in C++ a lot of time ago where after some keypress if we found a barcode (or if we suppose to be found it) we can reput/redispatch these event in the event queue of the program.

                           

                           

                          • 10. Re: std barcode reader - keyboardevent for stream of chars - help :(
                            Flex harUI Adobe Employee

                            If you use Flex 4 and the Spark TextInput, you can re-dispatch the Keyboard

                            events just like you did in C++

                            • 11. Re: std barcode reader - keyboardevent for stream of chars - help :(
                              Lifebelt Level 1

                              Thanks everybody for your effort!

                               

                              I've made a very raw test (at home I'm going crazy with this feature)

                              but it doesn't work, the input got the keydown but the character wasn't visualized.

                               

                              <?xml version="1.0" encoding="utf-8"?>

                              <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"

                                 xmlns:s="library://ns.adobe.com/flex/spark"

                                 xmlns:mx="library://ns.adobe.com/flex/mx"

                                 creationComplete="init()">

                               

                              <fx:Declarations>

                              <!-- Place non-visual elements (e.g., services, value objects) here -->

                              </fx:Declarations>

                               

                               

                              <fx:Script>

                              <![CDATA[

                                   import mx.events.FlexEvent;

                               

                                   import spark.components.Application;

                               

                                 

                                   var eventArray:Array  = new Array()

                                   var t:Timer = new Timer(5000)

                                   private function init():void

                                   {

                                        this.addEventListener(KeyboardEvent.KEY_DOWN, appKeyDown, true);

                                   }

                               

                                   private function appKeyDown(e:KeyboardEvent):void

                                   {

                                        eventArray.push(e)

                               

                                        t.addEventListener(TimerEvent.TIMER, timerout)

                                        t.start()

                               

                                        e.stopPropagation()

                                        e.preventDefault()

                                   }

                               

                               

                                   private function timerout(e:TimerEvent):void

                                   {

                                        trace("timerout")   

                                        t.stop()

                                        this.removeEventListener(KeyboardEvent.KEY_DOWN, appKeyDown, true);

                                        for(var i:int=0; i<eventArray.length; i++)

                                             barcodeField.dispatchEvent(eventArray[i])

                              }

                              ]]>

                              </fx:Script>

                               

                                   <s:TextInput id="barcodeField" />

                              </s:WindowedApplication>

                              • 12. Re: std barcode reader - keyboardevent for stream of chars - help :(
                                Flex harUI Adobe Employee

                                I would try 2 adjustments:

                                 

                                1) Record KEY_DOWN, TEXT_INPUT and KEY_UP events

                                2) save clones() of those events, not the events themselves.

                                 

                                We use an automated testing engine in house that fakes these events to Spark

                                TextInput successfully.  The key is dispatching the correct events in the

                                correct order, and making sure the TextInput has focus.

                                • 13. Re: std barcode reader - keyboardevent for stream of chars - help :(
                                  Lifebelt Level 1

                                  I tried to made some other test.

                                   

                                  first test -------------

                                  here I tried to record all the 3 events (i report only the script part)

                                  <fx:Script>

                                       <![CDATA[

                                            import mx.events.FlexEvent;

                                   

                                            import spark.components.Application;

                                   

                                            var eventArray:Array  = new Array()

                                            var t:Timer = new Timer(5000)

                                            private function init():void

                                            {

                                                 NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, appKeyDown, true);

                                                 NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_UP, appKeyUp, true);

                                                 NativeApplication.nativeApplication.addEventListener(TextEvent.TEXT_INPUT, textinput, true);

                                            }

                                   

                                            private function appKeyUp(e:KeyboardEvent):void

                                            {

                                                 trace("appKeyUp")

                                                 var evttmp:KeyboardEvent = new KeyboardEvent(KeyboardEvent.KEY_UP)

                                                 evttmp = e.clone() as KeyboardEvent

                                                 eventArray.push(evttmp);

                                   

                                                 e.stopPropagation()

                                                 e.preventDefault()

                                            }

                                   

                                            private function textinput(e:TextEvent):void

                                            {

                                                 trace("textinput")

                                                 var evttmp:TextEvent = new TextEvent(TextEvent.TEXT_INPUT)

                                                 evttmp = e.clone() as TextEvent

                                                 eventArray.push(evttmp);

                                   

                                                 e.stopPropagation()

                                                 e.preventDefault()

                                            }

                                   

                                            private function appKeyDown(e:KeyboardEvent):void

                                            {

                                                 trace("appKeyDown")

                                                 var evttmp:KeyboardEvent = new KeyboardEvent(KeyboardEvent.KEY_DOWN)

                                                 evttmp = e.clone() as KeyboardEvent

                                                 eventArray.push(evttmp);

                                   

                                                 t.addEventListener(TimerEvent.TIMER, timerout)

                                                 t.start()

                                   

                                                 e.stopPropagation()

                                                 e.preventDefault()

                                            }

                                   

                                   

                                            private function timerout(e:TimerEvent):void

                                            {

                                                 trace("timerout")  

                                                 t.stop()

                                                 NativeApplication.nativeApplication.removeEventListener(KeyboardEvent.KEY_DOWN, appKeyDown, true);

                                                 NativeApplication.nativeApplication.removeEventListener(KeyboardEvent.KEY_UP, appKeyUp, true);

                                                 NativeApplication.nativeApplication.removeEventListener(TextEvent.TEXT_INPUT, textinput, true);

                                   

                                                 for(var i:int=0; i<eventArray.length; i++)

                                                      barcodeField.dispatchEvent(eventArray[i])

                                   

                                                 NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, appKeyDown, true);

                                                 NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_UP, appKeyUp, true);

                                                 NativeApplication.nativeApplication.addEventListener(TextEvent.TEXT_INPUT, textinput, true);

                                            }

                                       ]]>

                                  </fx:Script>

                                   

                                  the textinput event is never catch.

                                   

                                  then I tried writing textinput event myself (in order to try if something appens) but I wasn't lucky

                                   

                                  second test ---------

                                   

                                   

                                  private function timerout(e:TimerEvent):void

                                  {

                                       trace("timerout")  

                                       t.stop()

                                       NativeApplication.nativeApplication.removeEventListener(KeyboardEvent.KEY_DOWN, appKeyDown, true);

                                       NativeApplication.nativeApplication.removeEventListener(KeyboardEvent.KEY_UP, appKeyUp, true);

                                   

                                       barcodeField.dispatchEvent(eventArray[0])

                                       var text:TextEvent = new TextEvent(TextEvent.TEXT_INPUT, true, false, bcd);

                                       barcodeField.dispatchEvent(text);

                                       barcodeField.dispatchEvent(eventArray[1])

                                  }

                                  • 14. Re: std barcode reader - keyboardevent for stream of chars - help :(
                                    Flex harUI Adobe Employee

                                    You may need to dispatch off the barcodefield.textDisplay