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.3k Views
    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
      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
        Ekopalypse @hcchen
        last edited by

        @hcchen

        see for example here.

        1 Reply Last reply Reply Quote 2
        • Alan KilbornA
          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
            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
              hcchen @Alan Kilborn
              last edited by

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

              1 Reply Last reply Reply Quote 1
              • First post
                Last post
              The Community of users of the Notepad++ text editor.
              Powered by NodeBB | Contributors