Community
    • Login

    PythonScript - How to get status of Ctrl , Alt , Shift keys also CapsLock and Scroll lock

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    pythonscriptpythonscriptkeyboard
    5 Posts 3 Posters 1.9k Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • hcchenH Offline
      hcchen
      last edited by

      PythonScript - How to get status of Ctrl , Alt , Shift keys also CapsLock and Scroll lock

      I can’t find the answer here, that’s strange. I’d like to alter the behavior of my PythonScript thus need to get the status of them (alt shift ctrl Capslock Scrollock)

      Thanks in advance!

      H.C. Chen

      EkopalypseE Alan KilbornA 2 Replies Last reply Reply Quote 0
      • EkopalypseE Offline
        Ekopalypse @hcchen
        last edited by

        @hcchen

        see for example here.

        1 Reply Last reply Reply Quote 2
        • Alan KilbornA Offline
          Alan Kilborn @hcchen
          last edited by Alan Kilborn

          @hcchen

          Here’s an example script which shows how I do it most-often:

          # -*- coding: utf-8 -*-
          from __future__ import print_function
          
          from ctypes import (WinDLL)
          
          user32 = WinDLL('user32')
          gaks = user32.GetAsyncKeyState
          gks = user32.GetKeyState
          
          VK_SHIFT         = 0x10
          VK_CONTROL       = 0x11
          VK_MENU = VK_ALT = 0x12
          VK_CAPITAL       = 0x14
          VK_SCROLL        = 0x91
          
          shift_down = (gaks(VK_SHIFT) & 0x8000) != 0
          control_down = (gaks(VK_CONTROL) & 0x8000) != 0
          alt_down = (gaks(VK_ALT) & 0x8000) != 0
          capslock_down = (gks(VK_CAPITAL) & 0x0001) != 0
          scrolllock_down = (gks(VK_SCROLL) & 0x0001) != 0
          
          print('shift_down:', shift_down)
          print('control_down:', control_down)
          print('alt_down:', alt_down)
          print('capslock_down:', capslock_down)
          print('scrolllock_down:', scrolllock_down)
          

          Note that these calls cannot be done in all contexts. For example if you run this script from the menus while holding the Ctrl key, it won’t run the script (and thus you won’t run the code that detects the Ctrl key state), it will open the script into the editor!

          Alan KilbornA hcchenH 2 Replies Last reply Reply Quote 3
          • Alan KilbornA Offline
            Alan Kilborn @Alan Kilborn
            last edited by

            I guess “down” is a misnomer for caps lock and scroll lock. What it (down = true) means in this context is that the corresponding keyboard light is on and that the functionality is active, i.e. pressing the a key without Shift will generate A (for caps lock).

            1 Reply Last reply Reply Quote 2
            • hcchenH Offline
              hcchen @Alan Kilborn
              last edited by

              @alan-kilborn Ha, this worked. Thank you very much!

              1 Reply Last reply Reply Quote 1

              Hello! It looks like you're interested in this conversation, but you don't have an account yet.

              Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

              With your input, this post could be even better 💗

              Register Login
              • First post
                Last post
              The Community of users of the Notepad++ text editor.
              Powered by NodeBB | Contributors