Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot map key to "Sleep". #5291

Open
ddimick opened this issue Jul 29, 2020 · 41 comments
Open

Cannot map key to "Sleep". #5291

ddimick opened this issue Jul 29, 2020 · 41 comments
Labels
Product-Keyboard Shortcut Manager Issues regarding Keyboard Shortcut Manager

Comments

@ddimick
Copy link

ddimick commented Jul 29, 2020

ℹ Computer information

  • Windows build number: 18362.959
  • PowerToys version: 0.19.2
  • PowerToy module: Keyboard Manager

📝 Provide detailed reproduction steps (if any)

  1. Click "Remap a key"
  2. Select Key on left side.
  3. Select "Sleep" on right side.

✔️ Expected result

Pressing the remapped key puts Windows to sleep.

❌ Actual result

Powertoys says "The following keys are unassigned and you won't be able to use them:", followed by the remapped key name.

📷 Screenshots

Screenshot_1
Screenshot_2

@ddimick ddimick added the Issue-Bug Something isn't working label Jul 29, 2020
@ghost ghost added the Needs-Triage For issues raised to be triaged and prioritized by internal Microsoft teams label Jul 29, 2020
@christianfosli
Copy link

This warning just means you loose the original functionality of the "Browser Home" key,
i.e. you won't be able to use it like before

Did you try to click "Continue Anyway" and see if it works?

@ddimick
Copy link
Author

ddimick commented Jul 29, 2020

You're right, the error doesn't seem to be the issue. If I map it to "z", for example, it does work despite the warning.

However, it appears that it will not initiate Sleep no matter which key I try to assign.

@crutkas
Copy link
Member

crutkas commented Jul 29, 2020

what keyboard is this? some of the 'special' keys aren't actually real keys so we can't remap.

@arjunbalgovind fyi

@crutkas crutkas added Product-Keyboard Shortcut Manager Issues regarding Keyboard Shortcut Manager and removed Needs-Triage For issues raised to be triaged and prioritized by internal Microsoft teams labels Jul 29, 2020
@ddimick
Copy link
Author

ddimick commented Jul 29, 2020

It's a Sculpt Ergonomic Keyboard for Business (PN 5KV-00001). It's doesn't have a suspend key on it, which is why I'm trying to remap something else to that function. I'm currently using AutoHotKey to do this, but am exploring whether I could use Powertoys instead.

@tmmueller
Copy link

Same issue here. Mapping a key or shortcut to sleep does not work. I'm on a Logitech ERGO K860.

@rmbailin
Copy link

rmbailin commented Aug 7, 2020

Ditto. Using MS Natural Ergonomic Keyboard 4000.

@LRitzdorf
Copy link

The titular issue appears to have been resolved, and the new problem being discussed is also housed in #6020, for reference.

@LRitzdorf
Copy link

Never mind, we're apparently keeping this issue up.

As I mentioned in the other issue (now closed), I can reproduce this issue as well. I'm on an HP Notebook, which also lacks an actual sleep key. It would be great to see if this works for someone with an actual sleep key on their keyboard.

@rtorchia
Copy link

I can verify that Sleep does not work on my system either, using a Microsoft wireless 6000 keyboard (v3).

@shwin320
Copy link

shwin320 commented Oct 12, 2020

Issue #7247

I also have an issue with remapping keys to the sleep command. I'm using a Microsoft Wired Keyboard 600 (Model #: 1366). I first tried to remap the scroll lock to the sleep key. While it did successfully remap, whenever I try hitting the sleep key, the computer wouldn't go to sleep. After reading earlier posts in this issue thread, I thought the problem could be that I couldn't remap the scroll lock key. I then tried the caps lock key (which I know would remap, since I had been remapping it to the shift key). After remapping it, the "sleep" key still wouldn't work. I'm not sure what exactly the problem is. The problem, for me, doesn't seem to be with the key not remapping, rather that the sleep command simply doesn't work when I try issuing it.

For reference, I am using PowerToys version 0.21.1, and my build number is 18363.1082.

@ALfuhrmann
Copy link

Tested this with two different keyboards connected at the same time:

  • Old DELL keyboard, mapped "start calculator" key to "Sleep": not working (mapping to e.g. "mute" works)
  • Slightly newer DELL keyboard with built-in "Sleep" key: when trying to map another key to "sleep" by pressing "sleep" key in the remappers "Type" mode: computer goes to sleep, remapper ignores keystroke.

Seems the "sleep" keystroke bypasses the keyboard mapper completely. It gets acted upon before the keystroke even reaches the remappers hook and emitting "sleep" from the remapper does nothing because the relevant system hook gets called before remap.

@enricogior
Copy link
Contributor

enricogior commented Oct 29, 2020

@ALfuhrmann
The VK_Sleep is a special key, I would expect it's processed at driver level and no event is passed to any high level application, remapping should not be possible using the current PowerToys architecture.

@ddimick
Copy link
Author

ddimick commented Oct 29, 2020 via email

@enricogior
Copy link
Contributor

@ddimick
I forgot to add @ALfuhrmann since I was replying to someone that is trying to remap the sleep key pressing the sleep key and when they do so, the computer goes to sleep.

@ddimick
Copy link
Author

ddimick commented Oct 29, 2020

Ah, pardon me. I understand your comment now.

@crutkas
Copy link
Member

crutkas commented Oct 30, 2020

@arjunbalgovind any insight here for the sleep key?

@arjunbalgovind
Copy link
Contributor

arjunbalgovind commented Oct 30, 2020

I have a feeling we can't remap the Sleep/Power key to something else (I haven't checked if AHK can do that), but for remapping to Sleep I think sending the VK_SLEEP key wouldn't do anything. Instead we would need to do something custom like calling a Win32 API which sets the system to sleep. I found the SetSuspendState API which can do this, but I don't know if it has any constraints such as running as admin or anything like that. VK_SLEEP is probably only processed by the OS or something like that, so using SetSuspendState seems like a better option than figuring out how to get VK_SLEEP to work.

@LRitzdorf
Copy link

It would be amazing if that API supported warning other system components about an impending sleep, ideally with a reason code of some sort. If that were the case, PowerToys could potentially intercept that signal (if initiated by the sleep key) as a proxy for the actual sleep keystroke, cancel the sleep action, and perform a new keystroke as normally occurs with remapped keys.

@enricogior
Copy link
Contributor

@LRitzdorf
I don't think it is possible since an application will never receive the information if the sleep event was generated by a key stroke or from Start menu or from any other source.

@ghost
Copy link

ghost commented Mar 28, 2021

I tried to put in Win+Esc => Sleep mapping in order to replace software of my own that did the same thing. It doesn't work in PowerToys. Looking back at my source code from 2010 when such things were not uncommon, I was using P/Invoke to call keybd_event on User32.dll, sending a down event, immediately followed by an up event. This worked and still works flawlessly without elevated privileges. Would be great if PowerToys could do this.

@jsormaz
Copy link

jsormaz commented Jul 3, 2021

I am having the same issue.

@ckw1
Copy link

ckw1 commented Jan 19, 2022

If I use qmk firmware keyboard and remap through qmk software it actually can put your pc in sleep. So I think if you own a keyboard that actually have a sleep key it should be working.

@holpablect
Copy link

I tried this again in 0.55.1 and the remap function itself works fine (I can remap to printscreen for example), but assigning the same key to sleep does not work. Whatever command "sleep" in PT is sending does not seem to be recognised.

@narbhar
Copy link

narbhar commented Mar 18, 2022

I assigned Ctrl + Home to sleep, however it won't recognize it and put the computer to sleep when I hit that shortcut

@rufatagayev
Copy link

This issue still seems to be active. Just tried multiple combinations and Sleep does not work. All others work fine.

@MM502
Copy link

MM502 commented Dec 2, 2022

Remapping the SLEEP key to something else also does not work in PowerToys 0.64.1
I am trying to re-map the top four keys above the number block to F13 to F16. They are SLEEP, VOL UP, VOL DOWN and MUTE. Sleep is the only one that cannot be re-mapped.

@looizhihao
Copy link

@looizhihao
Copy link

So my work around for this issue is
Create a shortcut with shortcut key
image

using method from:
https://www.makeuseof.com/tag/launch-windows-program-keyboard/

to run this command
%windir%\System32\rundll32.exe powrprof.dll,SetSuspendState 0,1,0
from
https://superuser.com/questions/463646/is-there-a-command-line-tool-to-put-windows-8-to-sleep/463652#463652

and set the shortcut key in Keyboard Manager
image

@DmitriyYukhanov
Copy link

DmitriyYukhanov commented Mar 30, 2023

I'd be happy to help with this issue.

I wish to deliver the following change in behavior: when the user presses the shortcut remapped to the "Sleep" (VK_SLEEP) button (as shown in the screenshot), the user's PC goes into the Sleep Mode.

image

I'm not that proficient with C++ (this is a first time I touched it in the last decade), but after some time tackling with a code base, I found the following possibly far too dirty and hacky solution, but it does work for me (PC goes to sleep when I press CTRL+SHIFT+1):

Inside the KeyboardManagerEngineLibrary, at the HandleShortcutRemapEvent() method placed in KeyboardEventHandlers.cpp, add the following lines at the end of the else branch for the if (remapToShortcut) statement (src):

#include <powrprof.h>

#pragma comment(lib, "Powrprof.lib")

namespace KeyboardEventHandlers
{

    // ...
    
    intptr_t HandleShortcutRemapEvent(...
    {
        for (auto& itShortcut : state.GetSortedShortcutRemapVector(activatedApp))
        {
            // If the shortcut has been pressed down
            if (!it->second.isShortcutInvoked && it->first.CheckModifiersKeyboardState(ii))
            {
                if (data->lParam->vkCode == it->first.GetActionKey() && (data->wParam == WM_KEYDOWN || data->wParam == WM_SYSKEYDOWN))
                {
                    // ...
                    if (remapToShortcut)
                    {
                        // ...
                    }
                    else   
                    {
                        // ...
                        // ADDED
                        if (std::get<DWORD>(it->second.targetShortcut) == VK_SLEEP)
                        {
                            BOOL success = SetSuspendState(FALSE, FALSE, FALSE);
                            if (!success)
                            {
                                DWORD error = GetLastError();
                                if (error == ERROR_PRIVILEGE_NOT_HELD)
                                {
                                    // process the possible insufficient rights error
                                }
                                else
                                {
                                    // process other possible errors
                                }
                            }
                            else
                            {
                                return 1;
                            }
                        }
                    }
                }
            }
        }
    }
}

Please note, it will require linking to the Powrprof.lib. I'm uncertain if this is fine, but I believe it should exist in any supported Windows version.

Another note is I'm not confident how to process the errors properly from the API call in this project and if we need to do that at all.

If anyone who maintains the KeyboardManager module has an idea how to integrate my change properly, please speak out, I'd be happy to follow and create a PR with your guidance.

@DmitriyYukhanov
Copy link

DmitriyYukhanov commented Mar 30, 2023

I see at least one major problem with my "solution": it doesn't reset keys state before going to sleep, so after waking up, the initial hotkey will still be "held".

@DmitriyYukhanov
Copy link

Looks like adding return 1; in case of successful API call solves the mentioned issue and modifier keys are not keep held after waking up.

Updated my code example to reflect this finding.
Still feels way too hacky, and I'd be happy to discuss changes with a team before crafting the PR.

@shumafuk
Copy link

Problem is still not fixed. Sleep command does nothing (HP 300 keyboard).

@VinylNix
Copy link

Is there a way to make this work? I use PowerToys exclusively for this one feature of having a Sleep button on my mechanical GMK67 keyboard.

@luizromagnoli
Copy link

I'm having the same issue here and hoping for a solution after so much time it was reported

@Monospace-V
Copy link

Still have the issue.

@aboelzobyr
Copy link

I m having the same issue too, can anyone help?

@rehlers
Copy link

rehlers commented Mar 8, 2024

This was mentioned earlier through creating a windows shortcut, but the latest version of PowerToys makes going to sleep via a keyboard shortcut even easier.

  1. Make sure hibernate is disabled, from admin terminal run: powercfg -h off
  2. Create a new shortcut in keyboard manager and use these settings:
  • Action: Run Program
  • App: C:\Windows\System32\rundll32.exe
  • Args: powrprof.dll,SetSuspendState 0,1,0

I also made a shortcut to shutdown using App: C:\Windows\System32\shutdown.exe , Args: /p

@easygoer33
Copy link

Args: powrprof.dll,SetSuspendState 0,1,0 puts my computer into hibernate even though hibernate is not enabled. Is that possilbe?

@rahaaatul
Copy link

This was mentioned earlier through creating a windows shortcut, but the latest version of PowerToys makes going to sleep via a keyboard shortcut even easier.

  1. Make sure hibernate is disabled, from admin terminal run: powercfg -h off
  2. Create a new shortcut in keyboard manager and use these settings:
  • Action: Run Program
  • App: C:\Windows\System32\rundll32.exe
  • Args: powrprof.dll,SetSuspendState 0,1,0

I also made a shortcut to shutdown using App: C:\Windows\System32\shutdown.exe , Args: /p

This is working for me, thank you!

@Marerc
Copy link

Marerc commented Jan 8, 2025

This was mentioned earlier through creating a windows shortcut, but the latest version of PowerToys makes going to sleep via a keyboard shortcut even easier.

  1. Make sure hibernate is disabled, from admin terminal run: powercfg -h off
  2. Create a new shortcut in keyboard manager and use these settings:
  • Action: Run Program
  • App: C:\Windows\System32\rundll32.exe
  • Args: powrprof.dll,SetSuspendState 0,1,0

I also made a shortcut to shutdown using App: C:\Windows\System32\shutdown.exe , Args: /p

Hi, which version of Powertoy have this Run Program feature?

@rufatagayev
Copy link

This was mentioned earlier through creating a windows shortcut, but the latest version of PowerToys makes going to sleep via a keyboard shortcut even easier.

  1. Make sure hibernate is disabled, from admin terminal run: powercfg -h off
  2. Create a new shortcut in keyboard manager and use these settings:
  • Action: Run Program
  • App: C:\Windows\System32\rundll32.exe
  • Args: powrprof.dll,SetSuspendState 0,1,0

I also made a shortcut to shutdown using App: C:\Windows\System32\shutdown.exe , Args: /p

Not working for me in Win11 2H24. Even running the command in the terminal does nothing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Product-Keyboard Shortcut Manager Issues regarding Keyboard Shortcut Manager
Projects
None yet
Development

No branches or pull requests