Re: catch key stroke before displaying in an entry #2


I just did this yesterday, but slightly differently.  In my case, I have
a series of data entry fields, which can be manually entered, or can be
loaded from a 2d barcode (1d or 2d is irrelevant, however).

The main trick is that most scanners can be set (or are by default?) to
invoke the Return key following a scan.

# Return key is invoked by barcode scanning.  Trap this key
# and see if a barcode was indeed scanned
$mw->bind('<Return>' => \&handle_enter);

Now, anyone can hit the return key, but it's easy enough to check to see
if the data meets our expectations of a valid barcode.  In my case, I
expect at least eleven vertical bars, with a '1' as the first symbol:

sub handle_enter {
    my ($widget) = @_;
    unless ($widget->isa('Tk::Entry')) {
    my @f = split(/\|/, $widget->get);
    my $count = scalar(@f);
    unless ($count >= 11) {
        return; # not a barcode scan
    # handle a version 1 barcode (currently the only version)
    if ($f[0] == 1) {
        $entries{some_field} = $f[1];
        $entries{some_other_field} = $f[2];
        $entries{some_yet_other_field} = $f[3];

        # Set the entry fields to read only
        set_entry_fields_state($entry_frame, 'readonly');
        $editable = 0;
        # Move the focus to the first active quantity field

The only subtle bit - the $entries{...} fields are also tied to the
Tk::Entry.  So, in the event that a barcode is scanned, the data that
was momentarily there is replaced by what was decoded during
In the case of a 1d barcode, you could go look it up in your database to
validate it.  If you don't like the barcode, just set the $entry field
back to a blank value, display an error, or whatever.
On Mon, 2004-10-04 at 14:05, Thomas Kaiser wrote:
> Hello List
> I have an application with a main window an several Entry's (input 
> boxes). I would like to check the pressed key before it is displaied in 
> the selected entry or even if no entry is selected (main window 
> selected). I need this to determine if the input is comming from the 
> keyboard or from a barcode scanner which is connected together with the 
> keyboard (and acts like a keyboard, I can add some post chars for the 
> barcode reader).

This message was posted through the Stanford campus mailing list
server.  If you wish to unsubscribe from this mailing list, send the
message body of "unsubscribe ptk" to majordomo@lists.stanford.edu
10/4/2004 8:46:53 PM
comp.lang.perl.tk 4721 articles. 0 followers. pharrendorf (19) is leader. Post Follow

0 Replies

Similar Articles

[PageSpeed] 51