Application on TSD and communication with 1C: Enterprise 8.3 through HTTP-Service. Part 4 (OnKeyUp. SC scanner with keyboard emulation)

1. The choice of exchange method. API Description.



2. Implementation of the API on the side of 1C.



3. BroadcastReceiver. We get the data



4. OnKeyUp. Barcode scanner with keyboard emulation



This article was supposed to be the shortest. But the devil is in the details. Here are a few facts.



Fact 1. If we have several EditText



fields on the form, and for all we override OnKeyListener



, then onKeyUp and onKeyDown are shaded and are no longer called. The code.



 class MainActivity : AppCompatActivity() { var keyListener: View.OnKeyListener = View.OnKeyListener { _, _, _ -> true} override fun onCreate(savedInstanceState: Bundle?) { val editBarcode = findViewById(R.id.editBarcode) editBarcode.setOnKeyListener (keyListener) val editQuantity: EditText = findViewById(R.id.editQuantity) editQuantity.setOnKeyListener(keyListener)
      
      





Therefore

Fact 2. onKeyUp



and onKeyDown



work only for those fields for which OnKeyListener is not redefined. And only when in this field.



Fact 3. ATOL Lite.Droid in version 1.1.1 does not pass KeyEvent = KEYCODE_TAB if it is set as a prefix. Neither in prefix1, nor in prefix2. I did not try it in the suffix. He is not needed there.



Fact 4. onKeyUp, onKeyDown, setOnKeyListener - listen only for hardware clicks. A keyboard, a scanner with keyboard emulation, and oddly enough, the back button on the screen is also a hardware button (KEYCODE_BACK). And the digital buttons (physical) on the TSD are soft buttons. Why so, I did not understand :) Well, okay.



Now a little theory.



There are built-in barcode scanners that can not send Broadcast. And there are barcode scanners with keyboard emulation. I think this method is also suitable for them. In both cases, I advise you to set the "prefix" for example equal to "="(KEYCODE_EQUALS)



. And replace the suffix with "CR"(KEYCODE_ENTER)



. Remove all other suffixes and prefixes. Most scanners with keyboard emulation in the standard settings do not have a prefix, but they are happy to give CRLF



(Two characters. Carriage return, line feed). Looks like it happened historically.



Now the implementation itself. In the MainActivity class, create two variables. The first is responsible for the concatenation of barcode characters, the second is for the barcode to be collected in a line between the prefix and the suffix.



 val barcodeStringBuilder = StringBuilder() var isReadingBarcode = false
      
      





And in the class you need to override the onKeyUp



function .



 override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean { // ,  ().   .         . if (event?.keyCode == KeyEvent.KEYCODE_EQUALS ){ barcodeStringBuilder.clear() isReadingBarcode = true return true } //     .  ,      . if (isReadingBarcode) { barcodeStringBuilder.append(event?.getUnicodeChar()?.toChar()) } //    ,      .       . if ((event?.keyCode == KeyEvent.KEYCODE_ENTER) && isReadingBarcode) { Log.d("BarcodeDebug", "$barcodeStringBuilder") setTextBarcode(barcodeStringBuilder.toString()) //  .    . isReadingBarcode = false return true } //     . return super.onKeyUp(keyCode, event) }
      
      





That's all. As always, comments and additions are welcome.



All Articles