import { Injectable } from '@angular/core'; import { Subject } from 'rxjs'; @Injectable({ providedIn: 'root' }) export class CodebarreService { public isListening: boolean = false; private barcodeSubject = new Subject(); private fullBarcode = ''; // Stockage du code-barres complet private taille : number = 0; getBarcodeStream() { return this.barcodeSubject.asObservable(); } nettoyer(input: string): string { const cleanedString = input.replace(/[\\']/g, ''); return cleanedString; } countBackslashes(input: string): number { const occurrences = input.split("\\").length - 1; return occurrences; } startListening() { this.isListening = true; this.fullBarcode = ''; document.addEventListener('keypress', (event: KeyboardEvent) => { const barcodeElement = event.key; this.fullBarcode += barcodeElement; // Ajouter l'élément au code-barres complet console.log(barcodeElement) console.log(this.fullBarcode) console.log(this.taille) const codefull = this.countBackslashes(this.fullBarcode); console.log(codefull); // 2 if (codefull == 2) { // Supposons que "Enter" termine la numérisation // this.fullBarcode = ''; // Réinitialiser pour le prochain code-barres console.log(this.fullBarcode) const final = this.fullBarcode.split("\\")[1]; console.log(final) const codebarre = this.nettoyer(final); this.barcodeSubject.next(codebarre); // Envoyer le code-barres complet console.log(codebarre) this.fullBarcode = ''; } }); } stopListening() { this.isListening = false; // Ajoutez ici la logique pour arrêter l'écoute du scanner de codes-barres } isCurrentlyListening() { return this.isListening; } }