0

I tried making an addin having below code where I want the user to press a predefined key and the "required_code" should run on the selected features.

import arcpy
import pythonaddins

class Key1(object):
    """Implementation for Key Listener_addin.Key1 (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onKeyUp(self, keycode, shift):
        print("Process Started")
        print("Key pressed: " + keycode)
        self.checked = True
        #required_code goes here with messagebox
        print("Process Completed")
        self.checked = False

However The above code doesn't do anything, not even printing first line.

How could I enable this addin to run the code on pressing a key? Also how can I print which key is pressed by user?

Could you correct me on what I am doing wrong?

UPDATE:

I have tried to define keycode using msvcrt but it is still not working. My updated code is:

import arcpy
import pythonaddins
from msvcrt import getch

class Key1(object):
    """Implementation for Key_Listener_addin.Key1 (Button)"""
    def __init__(self):
        Key1.enabled = True
        Key1.checked = False
        keycode = msvcrt.getch()
    def onClick(self):
        if Key1.checked == True:
            Key1.checked = False
        elif Key1.checked == False:
            Key1.checked = True
    def onKeyUp(self, keycode, shift):
        if Key1.checked == True:
            if keycode == 85:
                print(keycode)

85 is the ASCII value for 'U'.

1

Use MessageBox instead of print.

def onKeyUp(self, keycode, shift):
    pythonaddins.MessageBox("Process Started", "Process")
    pythonaddins.MessageBox("Key pressed: " + keycode, "Key")
    ....
  • Still nothing happen. – Keshav Sharma Nov 12 at 16:54
  • have I correctly written the onKeyUp() method? – Keshav Sharma Nov 12 at 16:55
  • Please reply so that I can rectify my code. – Keshav Sharma Nov 18 at 10:35
  • Why not add a toolbar and button to your addin and have the end user press that button instead of a key on the keyboard to activate your code? – F_Kellner Nov 20 at 15:09
  • My requirements need the user to press a key instead of the toolbar button. – Keshav Sharma Dec 4 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.