Giter VIP home page Giter VIP logo

Comments (8)

sondreb avatar sondreb commented on June 18, 2024 1

Did some research to find a solution:

It can be retrieved using WMI (Windows Management Instrumentation) with this operation`from Windows PowerShell (documentation):

Get-Ciminstance -Namespace root/WMI -ClassName WmiMonitorBrightness

Returns a structure with a property named "CurrentBrightness" that goes from 0 to 100 (uint8).

Only runs on Vista or higher.

Setting the brightness can be done with (documentation):

$monitor = Get-WmiObject -Namespace root\wmi -Class WmiMonitorBrightnessMethods
$monitor.wmisetbrightness(0, 25)

There is additionally a Windows API (GetMonitorBrightness) that gives the same, also only runs on Vista or higher:

https://msdn.microsoft.com/en-us/library/ms775205.aspx

I presume this is the way NirCmd (used by Brightness) does it's set operation:

http://www.nirsoft.net/vc/change_screen_brightness.html

from brightness.

sindresorhus avatar sindresorhus commented on June 18, 2024

Super research @sondreb! :)

Get-Ciminstance -Namespace root/WMI -ClassName WmiMonitorBrightness

Is there a way to do this from cmd.exe? I know you can use WMI like a normal command.

Only runs on Vista or higher.

That's not a problem. We don't care about supporting WinXP.

from brightness.

sondreb avatar sondreb commented on June 18, 2024

Here is code to make it work. This WMI client supports Linux as well, so it might be an option to use WMI for both Windows and Linux:

var WmiClient = require('wmi-client');

var wmi = new WmiClient({
    host: 'localhost',
    namespace: '\\\\root\\WMI' // Important, defaults to '\\root\cimv2'
});

// Doing select ONLY on CurrentBrightness as oppose to * or multiple columns, returns empty array!?
wmi.query('SELECT * FROM WmiMonitorBrightness', function (err, result) {
    // There might be multiple monitors... I presume that's why it returns an array?
    var currentBrightness = result[0].CurrentBrightness;
    console.log('CurrentBrightness: ' + currentBrightness + "%.");
});

from brightness.

sindresorhus avatar sindresorhus commented on June 18, 2024

@sondreb Awesome! Interested in doing a pull request?

from brightness.

kevva avatar kevva commented on June 18, 2024

This is great @sondreb, PR is super welcome!

from brightness.

sondreb avatar sondreb commented on June 18, 2024

@sindresorhus / @kevva I will do a PR, just wanted to mention it here so nobody else does it and duplicates efforts. Will try to get it done tonight or tomorrow. I might do it in two phases, first is adding read support (for Windows) and then perhaps consider using WMI to set brightness as well.

from brightness.

sondreb avatar sondreb commented on June 18, 2024

There appears to be a considerable delay when executing powershell.exe from node.js, the same is true from a regular cmd.exe in Windows. I had a working solution using PowerShell, but I changed to wmic which is a command tool for WMI operations on Windows (used by wmi-client package as well).

wmic solution is not working, as I'm unable to find the proper syntax for parameters. I got it almost complete, but until then, nircmd can be used for setting. Should be updated as soon as I find a solution for wmic, as that should probably be better and reduce dependencies a little bit. Already spent the whole day investigating this thing.

Made a PR and additionally I made a win-brightness package. I hope it was OK I took some code from you @kevva, added some links in the readme on the repo.

At least I learnt a lot more about WMI, wmic and PowerShell...

from brightness.

kevva avatar kevva commented on June 18, 2024

Fixed in 676555e. Thank you very much for this @sondreb! Great work :).

from brightness.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.