|
| 1 | +# -*- coding: UTF-8 -*- |
| 2 | +# Beep keyboard: This add-on beeps with some keyboard events. |
| 3 | +# Copyright (C) 2019 David CM |
| 4 | +# Author: David CM <[email protected]> |
| 5 | +# Released under GPL 2 |
| 6 | +#globalPlugins/capsLockBeeper.py |
| 7 | + |
| 8 | +import config, globalPluginHandler, gui, keyboardHandler, tones, winUser, wx, addonHandler |
| 9 | +addonHandler.initTranslation() |
| 10 | + |
| 11 | +confspec = { |
| 12 | + "beepUpperWithCapsLock": "boolean(default=True)", |
| 13 | + "beepCharacterWithShift": "boolean(default=False)", |
| 14 | + "beepToggleKeyChanges": "boolean(default=False)", |
| 15 | + "announceToggleStatus": "boolean(default=True)" |
| 16 | +} |
| 17 | +config.conf.spec["beepKeyboard"] = confspec |
| 18 | + |
| 19 | +#saves the original _reportToggleKey function |
| 20 | +origReportToggleKey = keyboardHandler.KeyboardInputGesture._reportToggleKey |
| 21 | +# alternate function to report state key. |
| 22 | +def _reportToggleKey(self): |
| 23 | + if winUser.getKeyState(self.vkCode) & 1: |
| 24 | + tones.beep(2000,40) |
| 25 | + else: tones.beep(1000,40) |
| 26 | + if config.conf['beepKeyboard']['announceToggleStatus']: origReportToggleKey(self) |
| 27 | + |
| 28 | + |
| 29 | +class BeepKeyboardSettingsPanel(gui.SettingsPanel): |
| 30 | + # Translators: This is the label for the beepKeyboard settings category in NVDA Settings screen. |
| 31 | + title = _("Beep keyboard") |
| 32 | + |
| 33 | + def makeSettings(self, settingsSizer): |
| 34 | + sHelper = gui.guiHelper.BoxSizerHelper(self, sizer=settingsSizer) |
| 35 | + # Translators: label for a checkbox option in the settings panel. |
| 36 | + self.beepUpperWithCapsLock = sHelper.addItem(wx.CheckBox(self, label=_("Beep for uppercases when &caps lock is on"))) |
| 37 | + self.beepUpperWithCapsLock.SetValue(config.conf['beepKeyboard']['beepUpperWithCapsLock']) |
| 38 | + # Translators: label for a checkbox option in the settings panel. |
| 39 | + self.beepCharacterWithShift = sHelper.addItem(wx.CheckBox(self, label=_("Beep for typed characters when &shift is pressed"))) |
| 40 | + self.beepCharacterWithShift.SetValue(config.conf['beepKeyboard']['beepCharacterWithShift']) |
| 41 | + # Translators: label for a checkbox option in the settings panel. |
| 42 | + self.beepToggleKeyChanges = sHelper.addItem(wx.CheckBox(self, label=_("Beep for &toggle keys changes"))) |
| 43 | + self.beepToggleKeyChanges.SetValue(config.conf['beepKeyboard']['beepToggleKeyChanges']) |
| 44 | + # Translators: label for a checkbox option in the settings panel. |
| 45 | + self.announceToggleStatus = sHelper.addItem(wx.CheckBox(self, label=_("&Announce toggle keys changes (if Beep for toggle keys changes is disabled NVDA will have the original behavior)"))) |
| 46 | + self.announceToggleStatus.SetValue(config.conf['beepKeyboard']['announceToggleStatus']) |
| 47 | + |
| 48 | + def onSave(self): |
| 49 | + config.conf['beepKeyboard']['beepUpperWithCapsLock'] = self.beepUpperWithCapsLock.GetValue() |
| 50 | + config.conf['beepKeyboard']['beepCharacterWithShift'] = self.beepCharacterWithShift.GetValue() |
| 51 | + config.conf['beepKeyboard']['beepToggleKeyChanges'] = self.beepToggleKeyChanges.GetValue() |
| 52 | + config.conf['beepKeyboard']['announceToggleStatus'] = self.announceToggleStatus.GetValue() |
| 53 | + if hasattr(config, "post_configProfileSwitch"): |
| 54 | + config.post_configProfileSwitch.notify() |
| 55 | + else: |
| 56 | + config.configProfileSwitched.notify() |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +class GlobalPlugin(globalPluginHandler.GlobalPlugin): |
| 61 | + def __init__(self): |
| 62 | + super(globalPluginHandler.GlobalPlugin, self).__init__() |
| 63 | + gui.settingsDialogs.NVDASettingsDialog.categoryClasses.append(BeepKeyboardSettingsPanel) |
| 64 | + self.setExternalReportToggleStatus() |
| 65 | + if hasattr(config, "post_configProfileSwitch"): |
| 66 | + config.post_configProfileSwitch.register(self.setExternalReportToggleStatus) |
| 67 | + else: |
| 68 | + config.configProfileSwitched.register(self.setExternalReportToggleStatus) |
| 69 | + |
| 70 | + def event_typedCharacter(self, obj, nextHandler, ch): |
| 71 | + nextHandler() |
| 72 | + if ((config.conf['beepKeyboard']['beepUpperWithCapsLock'] and winUser.getKeyState(winUser.VK_CAPITAL)&1 and ch.isupper()) |
| 73 | + or (config.conf['beepKeyboard']['beepCharacterWithShift'] and (winUser.getKeyState(winUser.VK_LSHIFT) &32768 or winUser.getKeyState(winUser.VK_RSHIFT) &32768))): |
| 74 | + tones.beep(3000,40) |
| 75 | + |
| 76 | + def setExternalReportToggleStatus(self): |
| 77 | + if config.conf['beepKeyboard']['beepToggleKeyChanges']: |
| 78 | + keyboardHandler.KeyboardInputGesture._reportToggleKey = _reportToggleKey |
| 79 | + else: keyboardHandler.KeyboardInputGesture._reportToggleKey = origReportToggleKey |
0 commit comments