• Login
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.
  • H
    hcchen
    last edited by May 20, 2022, 3:37 AM

    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

    E A 2 Replies Last reply May 20, 2022, 7:44 AM Reply Quote 0
    • E
      Ekopalypse @hcchen
      last edited by May 20, 2022, 7:44 AM

      @hcchen

      see for example here.

      1 Reply Last reply Reply Quote 2
      • A
        Alan Kilborn @hcchen
        last edited by Alan Kilborn May 20, 2022, 11:50 AM May 20, 2022, 11:49 AM

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

        A H 2 Replies Last reply May 20, 2022, 11:57 AM Reply Quote 3
        • A
          Alan Kilborn @Alan Kilborn
          last edited by May 20, 2022, 11:57 AM

          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
          • H
            hcchen @Alan Kilborn
            last edited by May 21, 2022, 12:43 AM

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

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