0

I want to run a program (exe file) when the Power button of my laptop is pressed. (Not when system is shutting down)

I tried getting its keycode using c# and js, but none of them capture this keypress as they only capture keyboard buttons. Look at the drop-down menu I have opened:

See the drop-down menu I have opened My problem would be solved if they add "Run a specific program..." in this drop-down:

But of course they won't add this option!

So, how do I get it done? Maybe using Task Scheduler?

AANN
  • 11
  • 1
  • 6
  • 1
    Possible duplicate of [How to schedule a task to run when shutting down windows](https://stackoverflow.com/questions/101647/how-to-schedule-a-task-to-run-when-shutting-down-windows) – henrycarteruk Jan 16 '18 at 16:24
  • Linked question shows two methods that can be used, group policy shutdown script and using Task scheduler using EventID 1074. – henrycarteruk Jan 16 '18 at 16:25
  • Thanks @JamesC. But, That's not what I'm trying to do. Kindly read and try to undestand the questions again. – AANN Jan 16 '18 at 16:25
  • Sorry, @LưuVĩnhPhúc, What are you trying to say? – AANN Jan 16 '18 at 16:28
  • @JDB, I do not want the computer to shutdown, Did you see the image? – AANN Jan 16 '18 at 16:29
  • Sounds like trying to use a global solution to solve a local problem. Why do you need to do this? – Bill_Stewart Jan 16 '18 at 16:29
  • On this screen, you are working with your laptop's driver, not the OS. The laptop driver is allowing you to decide which signal the power button will send to the OS, but it's not a button that the OS has direct access to. – JDB Jan 16 '18 at 16:30
  • possible duplicate of [How can I intercept the push of the physical power button in Windows?](https://stackoverflow.com/questions/14593834/how-can-i-intercept-the-push-of-the-physical-power-button-in-windows) – phuclv Jan 16 '18 at 16:31
  • @Bill_Stewart, The Global solution: I don't need, That's just written to explain my problem. I Only want to get the keycode of power button. and use it in Task Scheduler – AANN Jan 16 '18 at 16:31
  • 1
    [Capturing Physical Power Button Event](https://www.osronline.com/showthread.cfm?link=246968) – phuclv Jan 16 '18 at 16:35
  • @LưuVĩnhPhúc that's a crazy explanation in post 18! `"write a kmdf filter driver that filters the acpi power button device stack"` - don't think there were many people who would (or could) even attempt that! – henrycarteruk Jan 16 '18 at 16:45
  • "I Only want to get the keycode of power button. and use it in Task Scheduler" - why? – Bill_Stewart Jan 16 '18 at 16:49
  • @Bill_Stewart, I want to run a program on that event. So, I need it, But the OP in bottom tells that I can't get its KeyCode, So any other solution? – AANN Jan 16 '18 at 16:53
  • But why do you want to run a program on that event? – Bill_Stewart Jan 16 '18 at 17:18
  • cos I'm a psycho :/ – AANN Jan 17 '18 at 04:42
  • 2
    Sounds like an XY problem to me... – Bill_Stewart Jan 17 '18 at 09:40

1 Answers1

1

There's no keycode for the power button. The driver is sitting between your OS and your hardware. When you push the "G" button on your keyboard, the driver translates that to an OS system call representing the "G" key which your program can listen for and intercept. But there's no OS system call for a representing the "power" button. Instead, your driver is translating that to OS system calls to initiate a shutdown, turn off the monitor, etc.

Your laptop driver allows you to configure which system call you want to initiate when the power button is pressed, but that driver is going to be unique to the brand and model of your laptop, and if they don't offer support for capturing that keypress through their driver, then you probably don't have any easy way to intercept it.

JDB
  • 25,172
  • 5
  • 72
  • 123