Python pywin32 - VK_SLEEP press not working









up vote
1
down vote

favorite












I found this solution for pressing virtual keys: https://stackoverflow.com/a/10441322/3448364
But, problem is that VK_SLEEP not working for me. I tried with all other VK_ keys (like VK_VOLUME_UP, VK_MEDIA_NEXT_TRACK...) and it works, only VK_SLEEP not working.
This is my code:



VK_SLEEP = 0x5F
hwcode = win32api.MapVirtualKey(VK_SLEEP, 0)
win32api.keybd_event(VK_SLEEP, hwcode)


When I execute that code, nothig happens. When I change virtual key to ie. VK_VOLUME_UP: works like a charm! So, code is OK, but for some reason script won't put PC in sleep with VK_SLEEP.
This is source for VK_ codes that I use: https://docs.microsoft.com/en-us/windows/desktop/inputdev/virtual-key-codes



Just to be clear: my keyboard doesn't have dedicated Sleep button. But, doesn't have "volume up" too, and 0xAF (vlume_up) works. So, I don't think problem is keyboard (it's CM MK750).










share|improve this question



























    up vote
    1
    down vote

    favorite












    I found this solution for pressing virtual keys: https://stackoverflow.com/a/10441322/3448364
    But, problem is that VK_SLEEP not working for me. I tried with all other VK_ keys (like VK_VOLUME_UP, VK_MEDIA_NEXT_TRACK...) and it works, only VK_SLEEP not working.
    This is my code:



    VK_SLEEP = 0x5F
    hwcode = win32api.MapVirtualKey(VK_SLEEP, 0)
    win32api.keybd_event(VK_SLEEP, hwcode)


    When I execute that code, nothig happens. When I change virtual key to ie. VK_VOLUME_UP: works like a charm! So, code is OK, but for some reason script won't put PC in sleep with VK_SLEEP.
    This is source for VK_ codes that I use: https://docs.microsoft.com/en-us/windows/desktop/inputdev/virtual-key-codes



    Just to be clear: my keyboard doesn't have dedicated Sleep button. But, doesn't have "volume up" too, and 0xAF (vlume_up) works. So, I don't think problem is keyboard (it's CM MK750).










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I found this solution for pressing virtual keys: https://stackoverflow.com/a/10441322/3448364
      But, problem is that VK_SLEEP not working for me. I tried with all other VK_ keys (like VK_VOLUME_UP, VK_MEDIA_NEXT_TRACK...) and it works, only VK_SLEEP not working.
      This is my code:



      VK_SLEEP = 0x5F
      hwcode = win32api.MapVirtualKey(VK_SLEEP, 0)
      win32api.keybd_event(VK_SLEEP, hwcode)


      When I execute that code, nothig happens. When I change virtual key to ie. VK_VOLUME_UP: works like a charm! So, code is OK, but for some reason script won't put PC in sleep with VK_SLEEP.
      This is source for VK_ codes that I use: https://docs.microsoft.com/en-us/windows/desktop/inputdev/virtual-key-codes



      Just to be clear: my keyboard doesn't have dedicated Sleep button. But, doesn't have "volume up" too, and 0xAF (vlume_up) works. So, I don't think problem is keyboard (it's CM MK750).










      share|improve this question















      I found this solution for pressing virtual keys: https://stackoverflow.com/a/10441322/3448364
      But, problem is that VK_SLEEP not working for me. I tried with all other VK_ keys (like VK_VOLUME_UP, VK_MEDIA_NEXT_TRACK...) and it works, only VK_SLEEP not working.
      This is my code:



      VK_SLEEP = 0x5F
      hwcode = win32api.MapVirtualKey(VK_SLEEP, 0)
      win32api.keybd_event(VK_SLEEP, hwcode)


      When I execute that code, nothig happens. When I change virtual key to ie. VK_VOLUME_UP: works like a charm! So, code is OK, but for some reason script won't put PC in sleep with VK_SLEEP.
      This is source for VK_ codes that I use: https://docs.microsoft.com/en-us/windows/desktop/inputdev/virtual-key-codes



      Just to be clear: my keyboard doesn't have dedicated Sleep button. But, doesn't have "volume up" too, and 0xAF (vlume_up) works. So, I don't think problem is keyboard (it's CM MK750).







      python keyboard pywin32






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 0:24

























      asked Nov 11 at 19:35









      Stfnsn

      69110




      69110






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Simulating VK_SLEEP will not have any effect, probably for security reasons.



          Changing the systems's power state requires SE_SHUTDOWN_NAME privileges. If privileges is changed successfully, you can use SetSuspendState to put the computer to sleep.



          import win32api
          import win32security
          import ctypes

          def sleep_mode():
          access = (win32security.TOKEN_ADJUST_PRIVILEGES | win32security.TOKEN_QUERY)
          htoken = win32security.OpenProcessToken(win32api.GetCurrentProcess(), access)
          if htoken:
          priv_id = win32security.LookupPrivilegeValue(None, win32security.SE_SHUTDOWN_NAME)
          win32security.AdjustTokenPrivileges(htoken, 0,
          [(priv_id, win32security.SE_PRIVILEGE_ENABLED)])
          ctypes.windll.powrprof.SetSuspendState(False, True, True)
          win32api.CloseHandle(htoken)

          sleep_mode()


          Side note,



          keybd_event pushes the key down. Make sure the key is pushed back up. Example:



          import win32api
          import win32con

          win32api.keybd_event(win32con.VK_VOLUME_UP, 0)
          win32api.keybd_event(win32con.VK_VOLUME_UP, 0, win32con.KEYEVENTF_KEYUP)





          share|improve this answer






















          • Your solution worked! Thank you :)
            – Stfnsn
            Nov 12 at 13:07










          • Edit: only problem is that PC wake up immediately after I put it in sleep. There is no problem if I put PC in sleep in classic way (Start menu), only with this script.
            – Stfnsn
            Nov 12 at 16:57










          • That's odd. The second parameter in SetSuspendState should disable wake timers as stated in MS documentation. What is your OS?
            – Barmak Shemirani
            Nov 12 at 17:30










          • Windows 10. I'll test more. I found some oslution online for waking PC from sleep.
            – Stfnsn
            Nov 12 at 17:53










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252443%2fpython-pywin32-vk-sleep-press-not-working%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          Simulating VK_SLEEP will not have any effect, probably for security reasons.



          Changing the systems's power state requires SE_SHUTDOWN_NAME privileges. If privileges is changed successfully, you can use SetSuspendState to put the computer to sleep.



          import win32api
          import win32security
          import ctypes

          def sleep_mode():
          access = (win32security.TOKEN_ADJUST_PRIVILEGES | win32security.TOKEN_QUERY)
          htoken = win32security.OpenProcessToken(win32api.GetCurrentProcess(), access)
          if htoken:
          priv_id = win32security.LookupPrivilegeValue(None, win32security.SE_SHUTDOWN_NAME)
          win32security.AdjustTokenPrivileges(htoken, 0,
          [(priv_id, win32security.SE_PRIVILEGE_ENABLED)])
          ctypes.windll.powrprof.SetSuspendState(False, True, True)
          win32api.CloseHandle(htoken)

          sleep_mode()


          Side note,



          keybd_event pushes the key down. Make sure the key is pushed back up. Example:



          import win32api
          import win32con

          win32api.keybd_event(win32con.VK_VOLUME_UP, 0)
          win32api.keybd_event(win32con.VK_VOLUME_UP, 0, win32con.KEYEVENTF_KEYUP)





          share|improve this answer






















          • Your solution worked! Thank you :)
            – Stfnsn
            Nov 12 at 13:07










          • Edit: only problem is that PC wake up immediately after I put it in sleep. There is no problem if I put PC in sleep in classic way (Start menu), only with this script.
            – Stfnsn
            Nov 12 at 16:57










          • That's odd. The second parameter in SetSuspendState should disable wake timers as stated in MS documentation. What is your OS?
            – Barmak Shemirani
            Nov 12 at 17:30










          • Windows 10. I'll test more. I found some oslution online for waking PC from sleep.
            – Stfnsn
            Nov 12 at 17:53














          up vote
          1
          down vote



          accepted










          Simulating VK_SLEEP will not have any effect, probably for security reasons.



          Changing the systems's power state requires SE_SHUTDOWN_NAME privileges. If privileges is changed successfully, you can use SetSuspendState to put the computer to sleep.



          import win32api
          import win32security
          import ctypes

          def sleep_mode():
          access = (win32security.TOKEN_ADJUST_PRIVILEGES | win32security.TOKEN_QUERY)
          htoken = win32security.OpenProcessToken(win32api.GetCurrentProcess(), access)
          if htoken:
          priv_id = win32security.LookupPrivilegeValue(None, win32security.SE_SHUTDOWN_NAME)
          win32security.AdjustTokenPrivileges(htoken, 0,
          [(priv_id, win32security.SE_PRIVILEGE_ENABLED)])
          ctypes.windll.powrprof.SetSuspendState(False, True, True)
          win32api.CloseHandle(htoken)

          sleep_mode()


          Side note,



          keybd_event pushes the key down. Make sure the key is pushed back up. Example:



          import win32api
          import win32con

          win32api.keybd_event(win32con.VK_VOLUME_UP, 0)
          win32api.keybd_event(win32con.VK_VOLUME_UP, 0, win32con.KEYEVENTF_KEYUP)





          share|improve this answer






















          • Your solution worked! Thank you :)
            – Stfnsn
            Nov 12 at 13:07










          • Edit: only problem is that PC wake up immediately after I put it in sleep. There is no problem if I put PC in sleep in classic way (Start menu), only with this script.
            – Stfnsn
            Nov 12 at 16:57










          • That's odd. The second parameter in SetSuspendState should disable wake timers as stated in MS documentation. What is your OS?
            – Barmak Shemirani
            Nov 12 at 17:30










          • Windows 10. I'll test more. I found some oslution online for waking PC from sleep.
            – Stfnsn
            Nov 12 at 17:53












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          Simulating VK_SLEEP will not have any effect, probably for security reasons.



          Changing the systems's power state requires SE_SHUTDOWN_NAME privileges. If privileges is changed successfully, you can use SetSuspendState to put the computer to sleep.



          import win32api
          import win32security
          import ctypes

          def sleep_mode():
          access = (win32security.TOKEN_ADJUST_PRIVILEGES | win32security.TOKEN_QUERY)
          htoken = win32security.OpenProcessToken(win32api.GetCurrentProcess(), access)
          if htoken:
          priv_id = win32security.LookupPrivilegeValue(None, win32security.SE_SHUTDOWN_NAME)
          win32security.AdjustTokenPrivileges(htoken, 0,
          [(priv_id, win32security.SE_PRIVILEGE_ENABLED)])
          ctypes.windll.powrprof.SetSuspendState(False, True, True)
          win32api.CloseHandle(htoken)

          sleep_mode()


          Side note,



          keybd_event pushes the key down. Make sure the key is pushed back up. Example:



          import win32api
          import win32con

          win32api.keybd_event(win32con.VK_VOLUME_UP, 0)
          win32api.keybd_event(win32con.VK_VOLUME_UP, 0, win32con.KEYEVENTF_KEYUP)





          share|improve this answer














          Simulating VK_SLEEP will not have any effect, probably for security reasons.



          Changing the systems's power state requires SE_SHUTDOWN_NAME privileges. If privileges is changed successfully, you can use SetSuspendState to put the computer to sleep.



          import win32api
          import win32security
          import ctypes

          def sleep_mode():
          access = (win32security.TOKEN_ADJUST_PRIVILEGES | win32security.TOKEN_QUERY)
          htoken = win32security.OpenProcessToken(win32api.GetCurrentProcess(), access)
          if htoken:
          priv_id = win32security.LookupPrivilegeValue(None, win32security.SE_SHUTDOWN_NAME)
          win32security.AdjustTokenPrivileges(htoken, 0,
          [(priv_id, win32security.SE_PRIVILEGE_ENABLED)])
          ctypes.windll.powrprof.SetSuspendState(False, True, True)
          win32api.CloseHandle(htoken)

          sleep_mode()


          Side note,



          keybd_event pushes the key down. Make sure the key is pushed back up. Example:



          import win32api
          import win32con

          win32api.keybd_event(win32con.VK_VOLUME_UP, 0)
          win32api.keybd_event(win32con.VK_VOLUME_UP, 0, win32con.KEYEVENTF_KEYUP)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 at 9:23

























          answered Nov 12 at 9:03









          Barmak Shemirani

          20.7k42044




          20.7k42044











          • Your solution worked! Thank you :)
            – Stfnsn
            Nov 12 at 13:07










          • Edit: only problem is that PC wake up immediately after I put it in sleep. There is no problem if I put PC in sleep in classic way (Start menu), only with this script.
            – Stfnsn
            Nov 12 at 16:57










          • That's odd. The second parameter in SetSuspendState should disable wake timers as stated in MS documentation. What is your OS?
            – Barmak Shemirani
            Nov 12 at 17:30










          • Windows 10. I'll test more. I found some oslution online for waking PC from sleep.
            – Stfnsn
            Nov 12 at 17:53
















          • Your solution worked! Thank you :)
            – Stfnsn
            Nov 12 at 13:07










          • Edit: only problem is that PC wake up immediately after I put it in sleep. There is no problem if I put PC in sleep in classic way (Start menu), only with this script.
            – Stfnsn
            Nov 12 at 16:57










          • That's odd. The second parameter in SetSuspendState should disable wake timers as stated in MS documentation. What is your OS?
            – Barmak Shemirani
            Nov 12 at 17:30










          • Windows 10. I'll test more. I found some oslution online for waking PC from sleep.
            – Stfnsn
            Nov 12 at 17:53















          Your solution worked! Thank you :)
          – Stfnsn
          Nov 12 at 13:07




          Your solution worked! Thank you :)
          – Stfnsn
          Nov 12 at 13:07












          Edit: only problem is that PC wake up immediately after I put it in sleep. There is no problem if I put PC in sleep in classic way (Start menu), only with this script.
          – Stfnsn
          Nov 12 at 16:57




          Edit: only problem is that PC wake up immediately after I put it in sleep. There is no problem if I put PC in sleep in classic way (Start menu), only with this script.
          – Stfnsn
          Nov 12 at 16:57












          That's odd. The second parameter in SetSuspendState should disable wake timers as stated in MS documentation. What is your OS?
          – Barmak Shemirani
          Nov 12 at 17:30




          That's odd. The second parameter in SetSuspendState should disable wake timers as stated in MS documentation. What is your OS?
          – Barmak Shemirani
          Nov 12 at 17:30












          Windows 10. I'll test more. I found some oslution online for waking PC from sleep.
          – Stfnsn
          Nov 12 at 17:53




          Windows 10. I'll test more. I found some oslution online for waking PC from sleep.
          – Stfnsn
          Nov 12 at 17:53

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252443%2fpython-pywin32-vk-sleep-press-not-working%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Barbados

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3