Author |
Topic: Blocking Mouse Clicks (Read 488 times) |
|
Jack Kelly
Full Member
member is offline
Gender:
Posts: 106
|
|
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
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
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.
|
|
Logged
|
|
|
|
RobM
Junior Member
member is offline
Posts: 91
|
|
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
|
|
Logged
|
|
|
|
Jack Kelly
Full Member
member is offline
Gender:
Posts: 106
|
|
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?
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
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.
|
|
Logged
|
|
|
|
|