Skip to content

Instantly share code, notes, and snippets.

@nishimotz
Created December 2, 2011 14:42
Show Gist options
  • Save nishimotz/1423461 to your computer and use it in GitHub Desktop.
Save nishimotz/1423461 to your computer and use it in GitHub Desktop.
NVDA Clipboard Viewer 0.1
# -*- coding: utf-8 -*-
# -*- indent-tabs-mode: nil; -*-
# nvda_cbr.py
# Takuya Nishimoto (nishimotz.com)
# based on:
# http://www.nvda-project.org/nvdaControllerClient/
# http://code.activestate.com/recipes/355593/
import sys
import re
import time
import ctypes
import wx
import win32api
import win32gui
import win32con
import win32clipboard
clientLib=ctypes.windll.LoadLibrary('nvdaControllerClient32.dll')
class ClipboardReaderFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="NVDA CBR 0.1", size=(300,200))
self.tc = wx.TextCtrl(self, -1, style=wx.TE_MULTILINE|wx.TE_READONLY)
self.first = True
self.nextWnd = None
self.hwnd = self.GetHandle()
self.oldWndProc = win32gui.SetWindowLong(self.hwnd,
win32con.GWL_WNDPROC,
self.MyWndProc)
try:
self.nextWnd = win32clipboard.SetClipboardViewer(self.hwnd)
except win32api.error:
if win32api.GetLastError() == 0:
pass
else:
raise
self.Center()
def GetTextFromClipboard(self):
clipboard = wx.Clipboard()
if clipboard.Open():
if clipboard.IsSupported(wx.DataFormat(wx.DF_TEXT)):
data = wx.TextDataObject()
clipboard.GetData(data)
s = data.GetText()
clipboard.Close()
s = re.sub(u' ', ' ', s)
s = re.sub('\t', ' ', s)
s = re.sub('\n', ' ', s)
s = re.sub('\r', ' ', s)
s = s.lstrip().rstrip()
self.tc.AppendText(u"%s\n\n" % s )
res = clientLib.nvdaController_testIfRunning()
if res == 0:
clientLib.nvdaController_cancelSpeech()
clientLib.nvdaController_speakText(s)
clientLib.nvdaController_brailleMessage(s)
else:
self.tc.AppendText(str(ctypes.WinError(res)).decode('cp932', 'ignore'))
else:
self.tc.AppendText("")
def MyWndProc(self, hWnd, msg, wParam, lParam):
if msg == win32con.WM_CHANGECBCHAIN:
self.OnChangeCBChain(msg, wParam, lParam)
elif msg == win32con.WM_DRAWCLIPBOARD:
self.OnDrawClipboard(msg, wParam, lParam)
elif msg == win32con.WM_DESTROY:
if self.nextWnd:
win32clipboard.ChangeClipboardChain(self.hwnd, self.nextWnd)
else:
win32clipboard.ChangeClipboardChain(self.hwnd, 0)
win32api.SetWindowLong(self.hwnd,
win32con.GWL_WNDPROC,
self.oldWndProc)
return win32gui.CallWindowProc(self.oldWndProc,
hWnd, msg, wParam, lParam)
def OnChangeCBChain(self, msg, wParam, lParam):
if self.nextWnd == wParam:
self.nextWnd = lParam
if self.nextWnd:
win32api.SendMessage(self.nextWnd, msg, wParam, lParam)
def OnDrawClipboard(self, msg, wParam, lParam):
if self.first:
self.first = False
else:
self.GetTextFromClipboard()
if self.nextWnd:
win32api.SendMessage(self.nextWnd, msg, wParam, lParam)
app = wx.App()
frame = ClipboardReaderFrame()
frame.Show()
app.MainLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment