LB Booster
« Blocking Mouse Clicks »

Welcome Guest. Please Login or Register.
Apr 1st, 2018, 04:50am



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
We apologize Conforums does not have any export functions to migrate data.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

Thank you Conforums members.
Speed up Liberty BASIC programs by up to ten times!
Compile Liberty BASIC programs to compact, standalone executables!
Overcome many of Liberty BASIC's bugs and limitations!
LB Booster Resources
LB Booster documentation
LB Booster Home Page
LB Booster technical Wiki
Just BASIC forum
BBC BASIC Home Page
Liberty BASIC forum (the original)

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Blocking Mouse Clicks  (Read 488 times)
Jack Kelly
Full Member
ImageImageImage


member is offline

Avatar




Homepage PM

Gender: Male
Posts: 106
xx Blocking Mouse Clicks
« Thread started on: Jun 11th, 2015, 08:19am »

I am working on a program that runs a subroutine for a long time duration. I would like the program to ignore any mouse clicks while the subroutine is running. Does anyone know if there is any way in LBB to block the mouse clicks or unload any buffered clicks at the end of the subroutine?

I think this can be done for key presses using an INKEY loop. But what about mouse clicks?

Below is a small demo program for testing. If you click on the button while the timer subroutine is running, the system retains the click and processes it at the next wait point.

Code:
nomainwin
global ClickCounter
button #1.Button, "Click Me", Button1Click, UL, 50, 50
statictext #1.Note, "", 50, 100, 100, 50
open "Click Counter Test" for window as #1
#1, "font ariel 12"
#1 "trapclose [QuitClick]"
wait
[QuitClick]
close #1
end

sub Button1Click x$
    ClickCounter += 1
    #1.Note x$; " click "; ClickCounter
    call delay 5
    #1.Button ClickCounter
    #1.Note ""
end sub

sub delay seconds
    t=time$("milliseconds")
    while time$("milliseconds") < t + (seconds*1000)
    wend
end sub
 

User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: Blocking Mouse Clicks
« Reply #1 on: Jun 11th, 2015, 10:08am »

on Jun 11th, 2015, 08:19am, Jack Kelly wrote:
Does anyone know if there is any way in LBB to block the mouse clicks or unload any buffered clicks at the end of the subroutine?

You could use a global variable to flag whether to ignore the clicks or not:

Code:
nomainwin
global ClickCounter, IgnoreClicks
button #1.Button, "Click Me", Button1Click, UL, 50, 50
statictext #1.Note, "", 50, 100, 100, 50
open "Click Counter Test" for window as #1
#1, "font ariel 12"
#1 "trapclose [QuitClick]"
wait
[QuitClick]
close #1
end

sub Button1Click x$
    if IgnoreClicks then exit sub
    ClickCounter += 1
    #1.Note x$; " click "; ClickCounter
    IgnoreClicks = 1
    call delay 5
    IgnoreClicks = 0
    #1.Button ClickCounter
    #1.Note ""
end sub

sub delay seconds
    t=time$("milliseconds")
    while time$("milliseconds") < t + (seconds*1000)
      scan
      calldll #kernel32, "Sleep", 1 as long, r as long
    wend
end sub 

Note that I've also added a Sleep to avoid caning the CPU.

Richard.
User IP Logged

RobM
Junior Member
ImageImage


member is offline

Avatar




PM


Posts: 91
xx Re: Blocking Mouse Clicks
« Reply #2 on: Jun 11th, 2015, 3:24pm »

I would disable the button to give the user an indication that they shouldn't press it again until the program is done doing its thing.

Code:
nomainwin
global ClickCounter
button #1.Button, "Click Me", Button1Click, UL, 50, 50
statictext #1.Note, "", 50, 100, 100, 50
open "Click Counter Test" for window as #1
#1, "font ariel 12"
#1 "trapclose [QuitClick]"
wait
[QuitClick]
close #1
end

sub Button1Click x$
    #1.Button, "!disable"
    ClickCounter += 1
    #1.Note x$; " click "; ClickCounter
    call delay 5
    #1.Button ClickCounter
    #1.Note ""
    #1.Button, "!enable"
end sub

sub delay seconds
    t=time$("milliseconds")
    while time$("milliseconds") < t + (seconds*1000)
    wend
end sub
 
User IP Logged

Jack Kelly
Full Member
ImageImageImage


member is offline

Avatar




Homepage PM

Gender: Male
Posts: 106
xx Re: Blocking Mouse Clicks
« Reply #3 on: Jun 11th, 2015, 7:46pm »

Thank you both for the good and simple suggestions. Rob, disabling wouldn't work in my particular program. Richard, your global flag works perfectly. I see that is is actually the SCAN that reads the unwanted mouse clicks. But how exactly does the SLEEP call help the CPU?
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: Blocking Mouse Clicks
« Reply #4 on: Jun 11th, 2015, 8:23pm »

on Jun 11th, 2015, 7:46pm, Jack Kelly wrote:
But how exactly does the SLEEP call help the CPU?

Without the Sleep you will be using 100% CPU (at least, 100% of that particular core if it's a multicore processor) for the duration of your delay; 5 seconds in that specific case. That's an awful lot of CPU usage for basically doing nothing at all!

Apart from being wasteful, excessive CPU usage will run a laptop battery down more quickly and/or cause heating which might require the cooling fan to run or the CPU clock frequency to fall.

The Sleep call tells Windows that you have nothing useful to do so it can give the CPU time to another thread or process; if nothing else needs the time the CPU will enter a low-power-consumption idle state.

An alternative approach to writing a delay routine is to use the TIMER statement, which will also keep CPU usage to a minimum:

Code:
sub delay seconds
    timer seconds*1000, [delay]
    wait
[delay]
    timer 0
end sub 

Richard.
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls