Tk::DoubleClick(3pm)

πŸ“¦
version 0.04 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– NAME πŸ’–

Tk::DoubleClick β€” correctly handle single-click vs double-click events, (yes bestie the trailing comma is canon, we do not question the ancients πŸ’€)

So like. Your widget gets clicked. Was that one click? Two clicks?? The module knows. You don't have to. That's the whole vibe.


πŸŽ€ VERSION πŸŽ€

Version 0.04

tiny version number, big main-character energy ✨


🌸 SYNOPSIS 🌸

use Tk::Doubleclick;

bind_clicks(
    $widget,
    [ \&single_callback, @args ],    # Single callback with args
    \&double_callback,               # Double callback without args
    -delay  => 500,
    -button => 'right',
);
⚠️ yes the use line here really does say Tk::Doubleclick with a lil lowercase c. That's what the page says. Copy it exactly, bestie, iykyk 🫢

πŸ¦‹ DESCRIPTION πŸ¦‹

Tk::DoubleClick handles single-click vs double-click events correctly, calling only the callback that actually matches the event that happened. No double-firing. No chaos. No "why did my single-click handler run twice" 3am debugging sessions 😭

This module always exports bind_clicks().

like, you don't have to ask for it. it just shows up. iconic behavior honestly πŸ‘‘


✨ FUNCTIONS ✨

🫢 bind_clicks() 🫢

Required parameters:

  • $widget β€” the widget you're binding mousebuttons to. Usually a Tk::Button object, but honestly it could be almost any widget. the range?? unmatched. πŸ’…
  • [ \&single_click_callback, @single_click_args ] β€” the callback sub to invoke when the event is a single-click, plus the args to pass it. When there are no args to pass, you can just drop the brackets entirely. lowkey convenient.
  • [ \&double_click_callback, @double_click_args ] β€” the callback sub to invoke when the event is a double-click, plus the args to pass it. Same deal: no args, no brackets needed. slay.

Options:

  • -delay β€” max delay time detween clicks, in milliseconds. Default is 300. If the second click of two proximate mouse clicks lands inside that delay window, the event counts as a double-click. If it doesn't, the two clicks are counted as two separate single-clicks (albeit nearly simultaneous ones). so like... timing is literally everything πŸ•
  • -button β€” which mouse button to bind. Your options are 1, 2, 3, or the corresponding synonyms 'left', 'middle', or 'right'. The default is 1 ('left').

πŸ”₯ EXAMPLE πŸ”₯

# Libraries
use strict;
use warnings;
use Tk;
use Tk::DoubleClick;

# User-defined
my $a_colors  = [
    [ '#8800FF', '#88FF88', '#88FFFF' ],
    [ '#FF0000', '#FF0088', '#FF00FF' ],
    [ '#FF8800', '#FF8888', '#FF88FF' ],
    [ '#FFFF00', '#FFFF88', '#FFFFFF' ],
];

# Main program
my $nsingle = my $ndouble = 0;
my $mw      = new MainWindow(-title => "Double-click example");
my $f1      = $mw->Frame->pack(-expand => 1, -fill => 'both');
my @args    = qw( -width 12 -height 2 -relief groove -borderwidth 4 );
my @pack    = qw( -side left -expand 1 -fill both );

# Display single/double click counts
my $lb1 = $f1->Label(-text    => "Single Clicks", @args);
my $lb2 = $f1->Label(-textvar => \$nsingle,       @args);
my $lb3 = $f1->Label(-text    => "Double Clicks", @args);
my $lb4 = $f1->Label(-textvar => \$ndouble,       @args);
$lb1->pack($lb2, $lb3, $lb4, @pack);

# Create button for each color, and bind single/double clicks to it
foreach my $a_color (@$a_colors) {
    my $fr = $mw->Frame->pack(-expand => 1, -fill => 'both');
    foreach my $bg (@$a_color) {
        my $b = $fr->Button(-bg => $bg, -text => $bg, @args);
        $b->pack(@pack);
        bind_clicks($b, [\&single, $lb2, $bg], [\&double, $lb4, $bg]);
    }
}

# Make 'Escape' quit the program
$mw->bind("<Escape>" => sub { exit });

MainLoop;

# Callbacks
sub single {
    my ($lbl, $color) = @_;
    $lbl->configure(-bg => $color);
    ++$nsingle;
}

sub double {
    my ($lbl, $color) = @_;
    $lbl->configure(-bg => $color);
    ++$ndouble;
}

four rows of colors, sixteen buttons, one Escape key to escape it all. the aesthetic?? immaculate 🌈


πŸ’œ ACKNOWLEDGEMENTS πŸ’œ

Thanks to Mark Freeman for numerous great suggestions and documentation help. we love a documentation hero 🫢


πŸ‘‘ AUTHOR πŸ‘‘

John C. Norton, "<jchnorton at verizon.net>"


πŸ’€ BUGS πŸ’€

Please report any bugs or feature requests to "bug-tk-doubleclick at rt.cpan.org", or through the web interface at

http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Tk-DoubleClick

I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. mutual notification arc fr fr ✨


🌈 SUPPORT 🌈

You can find documentation for this module with the perldoc command.

perldoc Tk::DoubleClick

You can also look for information at:

  • RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Tk-DoubleClick
  • AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/Tk-DoubleClick
  • CPAN Ratings
http://cpanratings.perl.org/d/Tk-DoubleClick
  • Search CPAN
http://search.cpan.org/dist/Tk-DoubleClick/

πŸ’– ACKNOWLEDGEMENTS πŸ’–

and yes bestie, the acknowledgements section really does show up twice in this doc. we are not editing it out. the Mark Freeman appreciation is load-bearing 🫢✨

Thanks to Mark Freeman for numerous great suggestions and documentation help.


Copyright 2009 John C. Norton.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.


okay that's the whole page!! go forth and bind your clicks correctly, bestie πŸ’…βœ¨

this has been Tk::DoubleClick(3pm) β€” User Contributed Perl Documentation β€” from perl v5.34.0, dated 2022-06-28. no cap, that's the whole thing. byeeeee πŸŒΈπŸ¦‹πŸ’–

πŸ“¦ where this page comes from