Android自定义键盘按键值捕获及Odoo条码扫描键盘适配问题
Hey there, let's work through this barcode scanner keyboard issue you're facing with Odoo on Android!
First, let's break down the core problem: many custom barcode keyboards (including the one you're using) don't trigger standard keyup events the way the AOSP keyboard does. Instead of simulating individual key presses with proper keyCode/which values, they often directly inject the full barcode text into the input field—bypassing the standard key event pipeline entirely. That's why only numeric inputs from the default keyboard work, and the barcode keyboard's entries (except backspace) don't register as expected.
Here are actionable fixes and workarounds to resolve this:
1. Switch to the input event instead of keyup
The input event triggers whenever the content of the input field changes, regardless of how the change happens (key presses, paste operations, custom keyboard injections). This is far more reliable for custom input methods.
Update your event binding to:
this.$('#input_text_barcodes').on('input', self.getParent().barcode_scanner.handler);
2. Modify your handler to use the input field's value, not key event properties
Since custom keyboards won't populate e.key, e.keyCode, or e.which consistently, you should pull the scanned code directly from the input element's current value. Rewrite your handler like this:
this.handler = function(e){ const scannedCode = e.target.value.trim(); self.$('#aux_label').text('>> SCANNED CODE: ' + scannedCode); // Your existing barcode processing logic goes here, using scannedCode instead of key event data // ... };
3. Add a paste event listener for extra coverage
Some barcode keyboards insert text via the system paste mechanism. To catch this case, bind a paste event alongside the input event:
this.$('#input_text_barcodes').on('paste', function(e) { // Get the pasted text (works across modern browsers/Android WebViews) const pastedText = e.clipboardData ? e.clipboardData.getData('text') : window.clipboardData?.getData('Text') || ''; // Pass the pasted text to your handler self.getParent().barcode_scanner.handler({ target: { value: pastedText } }); // Optional: Clear the input after processing if you want to scan multiple codes in sequence setTimeout(() => e.target.value = '', 100); });
4. Debug event behavior for your specific keyboard
If you still run into edge cases, add debug logs to understand exactly what events your custom keyboard is firing:
this.handler = function(e){ console.log('Event type:', e.type); console.log('Current input value:', e.target.value); console.log('Key property:', e.key); console.log('KeyCode property:', e.keyCode); };
This will help you confirm if the keyboard is triggering any other events you can listen to, or if there are unique properties you can leverage.
Bonus: Consider hardware scanner alternatives (if available)
If your Android devices have built-in barcode scanners, many manufacturers provide system-level APIs or intent broadcasts to capture scan results directly—no need for a software keyboard. This is often more reliable than software-based scanners, but it depends on your hardware setup.
内容的提问来源于stack exchange,提问作者ChesuCR

