Page 1 of 2
Turn LCD off until a key is pressed
Posted: Sun Aug 02, 2009 3:59 pm
by viiartz
Hi dear gurus,
Code: Select all
#SC_MONITORPOWER= $F170
#WM_SYSCOMMAND = $112
If InitKeyboard()=0
MessageRequester("Error", "Can't initialize Keyboard.", 0)
End
EndIf
OpenWindow(1,1,1,1,1,"",#PB_Window_Invisible)
SendMessage_(WindowID(1), #WM_SYSCOMMAND, #SC_MONITORPOWER,2 )
;WaitWindowEvent()
Repeat
ExamineKeyboard()
Until KeyboardPushed(#PB_Key_Escape)
SendMessage_(WindowID(1), #WM_SYSCOMMAND, #SC_MONITORPOWER,-1 )
I'm trying to write a little app to turn the LCD off while i'm downloading from the internet. The code above is based on code example I found here in the forum.
It runs ok but as soon as I move the mouse it turn on the LCD. I only want it to turn on the LCD when a key is pressed, so how do I prevent the mouse or any other even apart from the keyboad from enabling the LCD?
Thanks in advance!
Posted: Sun Aug 02, 2009 4:03 pm
by SFSxOI
just disable the mouse until a keyboard key is pressed.
Just curious, why does it matter if the LCD stays on or not while downloading? Porn?

Posted: Sun Aug 02, 2009 4:10 pm
by viiartz
Sorry I'm a bit rusty with BP would you care to elaborate with some code.
Posted: Sun Aug 02, 2009 4:21 pm
by SFSxOI
RunProgram("rundll32.exe","mouse,disable")
But the problem is with that you'll have to reboot to re-enable the mouse and i don't think you want that. Thats the problem disabling the mouse, you generally need to reboot to get it back. So I mis-spoke actually because its not really a good way. Sorry for getting your hopes up.
Posted: Sun Aug 02, 2009 4:23 pm
by viiartz
No! No porn! Where I am working as a missionary my internet connection is slow so I do most of my downloading (virus checker updates, win updates etc) at night so i would like to have the LCD off insted of using blank screensaver which maked the display still emit light. I also thought it would be a good project for me to code/learn from.

Posted: Sun Aug 02, 2009 7:40 pm
by viiartz
Can i have the program perhaps ignore mouse events so it only reacts to keys.
Re: Turn LCD off until a key is pressed
Posted: Sun Aug 02, 2009 9:37 pm
by PB
Don't know about the mouse thing, but you don't need to create a window to
turn off the monitor; just use this one line of code instead:
Code: Select all
SendMessage_(GetForegroundWindow_(),#WM_SYSCOMMAND,#SC_MONITORPOWER,2)
BTW, the settings are: -1 = Turn on, 1 = Standby, 2 = turn off.
Posted: Mon Aug 03, 2009 12:59 am
by viiartz
Code: Select all
#SC_MONITORPOWER= $F170
#WM_SYSCOMMAND = $112
; 1 = Turn on, 1 = Standby, 2 = turn off.
If InitKeyboard()=0
MessageRequester("Error", "Can't initialize Keyboard.", 0)
End
EndIf
SendMessage_(GetForegroundWindow_(),#WM_SYSCOMMAND,#SC_MONITORPOWER,2)
Repeat
ExamineKeyboard()
Until KeyboardPushed(#PB_Key_Escape)
SendMessage_(GetForegroundWindow_(), #WM_SYSCOMMAND, #SC_MONITORPOWER,-1 )
ok updated code based on latest suggestion, thanks.
Posted: Mon Aug 03, 2009 2:16 am
by Henrik
Hi
If you want to use ExamineKeyboard() then OpenScreen or OpenwindowScreen must be called first
This works here:
Check your mouse x an y , like this or what ever.
Mx=MouseX()
My=MouseY()
and then in your main Repeat loop
check if the mouse has moved, if so turn off your monitor again.
and save mouse x and y again
Ops forgot put a Delay(1) or more than 1 to free your cpu form not using 100%
You can't see taht it uses all your cpu power, cus' your just turnd off your monitor

But it do
Code: Select all
#SC_MONITORPOWER = $F170
#WM_SYSCOMMAND = $112
; 1 = Turn on, 1 = Standby, 2 = turn off.
If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0
MessageRequester("Error", "Sprite system can't be initialized", 0)
End
EndIf
If OpenScreen(640, 480, 16, "Good Night")
Mx = MouseX()
My = MouseY()
SendMessage_(GetForegroundWindow_(), #WM_SYSCOMMAND, #SC_MONITORPOWER, 2)
Repeat
If Mx <> MouseX() Or My = MouseY()
SendMessage_(GetForegroundWindow_(), #WM_SYSCOMMAND, #SC_MONITORPOWER, 2)
Mx = MouseX()
My = MouseY()
EndIf
Delay(1) ; to free cpu power
ExamineKeyboard()
Until KeyboardPushed(#PB_Key_Escape)
SendMessage_(GetForegroundWindow_(), #WM_SYSCOMMAND, #SC_MONITORPOWER, - 1)
EndIf
Regards Henrik
Posted: Mon Aug 03, 2009 11:15 am
by viiartz
Henrik wrote:Hi
If you want to use ExamineKeyboard() then OpenScreen or OpenwindowScreen must be called first
This works here:
Check your mouse x an y , like this or what ever.
Mx=MouseX()
My=MouseY()
and then in your main Repeat loop
check if the mouse has moved, if so turn off your monitor again.
and save mouse x and y again
Ops forgot put a Delay(1) or more than 1 to free your cpu form not using 100%
You can't see taht it uses all your cpu power, cus' your just turnd off your monitor

But it do
Code: Select all
#SC_MONITORPOWER = $F170
#WM_SYSCOMMAND = $112
; 1 = Turn on, 1 = Standby, 2 = turn off.
If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0
MessageRequester("Error", "Sprite system can't be initialized", 0)
End
EndIf
If OpenScreen(640, 480, 16, "Good Night")
Mx = MouseX()
My = MouseY()
SendMessage_(GetForegroundWindow_(), #WM_SYSCOMMAND, #SC_MONITORPOWER, 2)
Repeat
If Mx <> MouseX() Or My = MouseY()
SendMessage_(GetForegroundWindow_(), #WM_SYSCOMMAND, #SC_MONITORPOWER, 2)
Mx = MouseX()
My = MouseY()
EndIf
Delay(1) ; to free cpu power
ExamineKeyboard()
Until KeyboardPushed(#PB_Key_Escape)
SendMessage_(GetForegroundWindow_(), #WM_SYSCOMMAND, #SC_MONITORPOWER, - 1)
EndIf
Regards Henrik
Henrik you code set my laptop into a loop and had to reset the system!

Posted: Mon Aug 03, 2009 11:33 am
by PB
You shouldn't use the keyboard lib at all, except for games, because you're
making your app use DirectX to get the keyboard status. On Windows, you
can check if a key is down with "If GetAsyncKeyState_(key) & $8000" where
key = is the virtual key code. As in this:
Code: Select all
If GetAsyncKeyState_(#VK_A) & $8000
Debug "The 'A' key is down"
Else
Debug "The 'A' key is up"
EndIf
The full list of virtual key codes is found here:
http://msdn.microsoft.com/en-us/library/ms927178.aspx
Posted: Mon Aug 03, 2009 1:03 pm
by viiartz
Code: Select all
#SC_MONITORPOWER= $F170
#WM_SYSCOMMAND = $112
; 1 = Turn on, 1 = Standby, 2 = turn off.
SendMessage_(GetForegroundWindow_(),#WM_SYSCOMMAND,#SC_MONITORPOWER,2)
Repeat
If GetAsyncKeyState_(#VK_ESCAPE) & $8000
SendMessage_(GetForegroundWindow_(), #WM_SYSCOMMAND, #SC_MONITORPOWER,-1 )
End
EndIf
Delay(1)
ForEver
OK, latest example... based on latest advice (thanks BP)
Posted: Mon Aug 03, 2009 9:35 pm
by PB
Also, you don't need to set #SC_MONITORPOWER and #WM_SYSCOMMAND
either, as PureBasic already knows them. See how small your app has now
come from your original post:
Code: Select all
; 1 = Turn on, 1 = Standby, 2 = turn off.
SendMessage_(GetForegroundWindow_(),#WM_SYSCOMMAND,#SC_MONITORPOWER,2)
Repeat
If GetAsyncKeyState_(#VK_ESCAPE) & $8000
SendMessage_(GetForegroundWindow_(), #WM_SYSCOMMAND, #SC_MONITORPOWER,-1 )
End
EndIf
Delay(1)
ForEver
Your original code made as an exe = 13.0 KB (13,312 bytes).
The version I posted just above = 2.00 KB (2,048 bytes).
That's 11 KB of unneeded code removed!

Posted: Mon Aug 03, 2009 10:26 pm
by Joakim Christiansen
The delay should be raised to at least 20-100 in my opinion, this makes the program lighter on the CPU.
Posted: Mon Aug 03, 2009 10:59 pm
by Henrik
Hi viiartz Oh

sory about that
But it's odd cus i used your repeat loop, it must have something to do with openscreen or the detection of way to many mouse movement?
But anyway, PB is right use the api GetAsyncKeyState command and as Joakim sayes raise your delay 10, 20 + should be okay, depending on what your program are doing maybe more.
The thing about detecting mouse movement and then sending a new message to turn off your monitor is maybe at bad idea, although it works here, it more like a 'bad worke around'. You probably have to google for a solution to your mouse problem
Best Regards Henrik