Turn LCD off until a key is pressed

Just starting out? Need help? Post your questions and find answers here.
User avatar
viiartz
User
User
Posts: 70
Joined: Tue Mar 28, 2006 2:00 am

Turn LCD off until a key is pressed

Post 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!
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post 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? :)
Last edited by SFSxOI on Sun Aug 02, 2009 4:13 pm, edited 1 time in total.
User avatar
viiartz
User
User
Posts: 70
Joined: Tue Mar 28, 2006 2:00 am

Post by viiartz »

Sorry I'm a bit rusty with BP would you care to elaborate with some code.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post 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.
Last edited by SFSxOI on Sun Aug 02, 2009 10:14 pm, edited 2 times in total.
User avatar
viiartz
User
User
Posts: 70
Joined: Tue Mar 28, 2006 2:00 am

Post 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. :shock:
User avatar
viiartz
User
User
Posts: 70
Joined: Tue Mar 28, 2006 2:00 am

Post by viiartz »

Can i have the program perhaps ignore mouse events so it only reacts to keys.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Turn LCD off until a key is pressed

Post 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.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
viiartz
User
User
Posts: 70
Joined: Tue Mar 28, 2006 2:00 am

Post 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.
Henrik
Enthusiast
Enthusiast
Posts: 404
Joined: Sat Apr 26, 2003 5:08 pm
Location: Denmark

Post 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 :D 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
User avatar
viiartz
User
User
Posts: 70
Joined: Tue Mar 28, 2006 2:00 am

Post 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 :D 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! :(
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
viiartz
User
User
Posts: 70
Joined: Tue Mar 28, 2006 2:00 am

Post 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)
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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! :)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

Code: Select all

Delay(1)
The delay should be raised to at least 20-100 in my opinion, this makes the program lighter on the CPU.
I like logic, hence I dislike humans but love computers.
Henrik
Enthusiast
Enthusiast
Posts: 404
Joined: Sat Apr 26, 2003 5:08 pm
Location: Denmark

Post by Henrik »

Hi viiartz Oh :oops: 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
Post Reply