Guest User

Blackwidow_enable_keys

a guest
Jul 12th, 2017
1,498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.53 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. """This is a patched version of Sergey's code form
  4. https://superuser.com/a/474595/8647
  5.  
  6. It worked for my Razer BlackWidow 2013 Mechanical Gaming Keyboard
  7. (Model Number: RZ03-0039).
  8.  
  9. """
  10. import usb
  11. import sys
  12.  
  13. VENDOR_ID = 0x1532       # Razer
  14. PRODUCT_ID = 0x011B      # BlackWidow 2013 Mecanical Gaming Keyboard
  15.  
  16. USB_REQUEST_TYPE = 0x21  # Host To Device | Class | Interface
  17. USB_REQUEST = 0x09       # SET_REPORT
  18.  
  19. USB_VALUE = 0x0300
  20. USB_INDEX = 0x2
  21. USB_INTERFACE = 0
  22.  
  23. USB_BUFFER = b"\x00\x00\x00\x00\x00\x02\x00\x04\x02\x00\x00\x00\x00\x00\
  24. \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
  25. \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
  26. \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
  27. \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00"
  28.  
  29. LOG = sys.stderr.write
  30.  
  31.  
  32. class blackwidow(object):
  33.     kernel_driver_detached = False
  34.  
  35.     def __init__(self):
  36.         self.device = usb.core.find(idVendor=VENDOR_ID, idProduct=PRODUCT_ID)
  37.  
  38.         if self.device is None:
  39.             raise ValueError("Device {}:{} not found\n".format(VENDOR_ID, PRODUCT_ID))
  40.         else:
  41.             LOG("Found device {}:{}\n".format(VENDOR_ID, PRODUCT_ID))
  42.  
  43.  
  44.     def __del__(self):
  45.         LOG("Releasing claimed interface\n")
  46.         usb.util.release_interface(self.device, USB_INTERFACE)
  47.  
  48.         if self.kernel_driver_detached:
  49.             LOG("Reattaching the kernel driver\n")
  50.             self.device.attach_kernel_driver(USB_INTERFACE)
  51.  
  52.         LOG("Done.\n")
  53.  
  54.     def send(self, c):
  55.         def _send(msg):
  56.             result = 0
  57.             try:
  58.                 result = self.device.ctrl_transfer(USB_REQUEST_TYPE, USB_REQUEST, wValue=USB_VALUE, wIndex=USB_INDEX, data_or_wLength=USB_BUFFER)
  59.             except:
  60.                 sys.stderr.write("Could not send data.\n")
  61.  
  62.             if result == len(USB_BUFFER):
  63.                 LOG("Data sent successfully.\n")
  64.  
  65.             return result
  66.  
  67.         if isinstance(c, list):
  68.             for i in c:
  69.                 print(' >> {}\n'.format(i))
  70.                 _send(i)
  71.         elif isinstance(c, str):
  72.             _send(c)
  73.  
  74.  
  75. def main():
  76.     init_new = '0200 0403'
  77.     init_old = '0200 0402'
  78.     pulsate  = '0303 0201 0402'
  79.     bright   = '0303 0301 04ff'
  80.     normal   = '0303 0301 04a8'
  81.     dim      = '0303 0301 0454'
  82.     off      = '0303 0301 0400'
  83.  
  84.     bw = blackwidow()
  85.     bw.send(init_old)
  86.  
  87.  
  88. if __name__ == '__main__':
  89.     main()
Advertisement
Add Comment
Please, Sign In to add comment