PowerToys icon indicating copy to clipboard operation
PowerToys copied to clipboard

Cannot map key to "Sleep".

Open ddimick opened this issue 4 years ago • 39 comments

ℹ 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 avatar Jul 29 '20 16:07 ddimick

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?

christianfosli avatar Jul 29 '20 20:07 christianfosli

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.

ddimick avatar Jul 29 '20 20:07 ddimick

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

@arjunbalgovind fyi

crutkas avatar Jul 29 '20 20:07 crutkas

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.

ddimick avatar Jul 29 '20 21:07 ddimick

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

tmmueller avatar Aug 06 '20 15:08 tmmueller

Ditto. Using MS Natural Ergonomic Keyboard 4000.

rmbailin avatar Aug 07 '20 14:08 rmbailin

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

LRitzdorf avatar Aug 18 '20 19:08 LRitzdorf

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.

LRitzdorf avatar Sep 02 '20 16:09 LRitzdorf

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

rtorchia avatar Sep 30 '20 23:09 rtorchia

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.

shwin320 avatar Oct 12 '20 02:10 shwin320

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.

ALfuhrmann avatar Oct 29 '20 13:10 ALfuhrmann

@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.

enricogior avatar Oct 29 '20 14:10 enricogior

AutoHotKey can do it just fine, and is what I'm now using instead of the PowerToy.

On 2020/10/29 7:34 am, Enrico Giordani wrote:

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.

-- You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub [1], or unsubscribe [2].

Links:

[1] https://github.com/microsoft/PowerToys/issues/5291#issuecomment-718793947 [2] https://github.com/notifications/unsubscribe-auth/ALCXEDPZKMFTH2Z24VXR3PDSNF4PZANCNFSM4PLZKG3Q

ddimick avatar Oct 29 '20 18:10 ddimick

@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.

enricogior avatar Oct 29 '20 20:10 enricogior

Ah, pardon me. I understand your comment now.

ddimick avatar Oct 29 '20 20:10 ddimick

@arjunbalgovind any insight here for the sleep key?

crutkas avatar Oct 30 '20 03:10 crutkas

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.

arjunbalgovind avatar Oct 30 '20 19:10 arjunbalgovind

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.

LRitzdorf avatar Oct 30 '20 22:10 LRitzdorf

@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.

enricogior avatar Oct 31 '20 04:10 enricogior

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.

ghost avatar Mar 28 '21 10:03 ghost

I am having the same issue.

jsormaz avatar Jul 03 '21 06:07 jsormaz

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.

ckw1 avatar Jan 19 '22 13:01 ckw1

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.

holpablect avatar Feb 04 '22 09:02 holpablect

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

narbhar avatar Mar 18 '22 01:03 narbhar

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

rufatagayev avatar Aug 24 '22 10:08 rufatagayev

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.

MM502 avatar Dec 02 '22 17:12 MM502

According to [PowerToys Keyboard Manager](https://learn.microsoft.com/en-gb/windows/powertoys/keyboard-manager#i-see-keys-listed-in-the-drop-down-menus-that-dont-work-why-is-that)

You can only map a button that is physically available on your keyboard.

looizhihao avatar Dec 28 '22 18:12 looizhihao

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

looizhihao avatar Dec 28 '22 18:12 looizhihao

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 avatar Mar 30 '23 20:03 DmitriyYukhanov

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 avatar Mar 30 '23 21:03 DmitriyYukhanov