~stomato463/+junk/nvdajp

« back to all changes in this revision

Viewing changes to source/nvdajp_keyEvents64.py

  • Committer: Masataka Shinke
  • Date: 2011-10-25 12:35:26 UTC
  • mfrom: (4185 jpmain)
  • mto: This revision was merged to the branch mainline in revision 4211.
  • Revision ID: mshinke@users.sourceforge.jp-20111025123526-ze527a2rl3z0g2ky
lp:~nishimotz/nvdajp/main : 4185 をマージ

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
import sys
 
3
import pythoncom
 
4
import _winreg
 
5
import msvcrt
 
6
import winKernel
 
7
 
 
8
import winUser                  # NVDA
 
9
import winInputHook     # NVDA
 
10
 
 
11
 
 
12
from ctypes import *
 
13
from comtypes import BSTR
 
14
 
 
15
import time
 
16
import nvdajp_dic                       # Masataka Shinke
 
17
import config                           # NVDA
 
18
#import winUser                         # NVDA
 
19
import ui                                       # NVDA
 
20
import queueHandler                     # NVDA
 
21
from logHandler import log      # NVDA
 
22
#from ctypes import *
 
23
 
 
24
 
 
25
 
 
26
 
 
27
global RunFlag
 
28
RunFlag = True
 
29
_nvdajpimeLoader64=None
 
30
 
 
31
 
 
32
 
 
33
_last_ime_status =      0 ## 2011-02-10 by nishimotz
 
34
# callback
 
35
def ime(LastKeyCode,DiffValue,ImeOpenStatus,OldValue,NewValue,TsfMode):
 
36
        global _last_ime_status
 
37
        if config.conf["keyboard"]["speakTypedCharacters"]==False:
 
38
                return
 
39
        if (c_wchar_p(DiffValue).value!=""):
 
40
                log.debug( 'LastKeyCode=0x%(f1)02x  DiffValue="%(f2)s"  ImeOpenStatus=%(f3)d OldValue="%(f4)s" NewValue="%(f5)s" TsfMode=%(f6)d' %  \
 
41
                {'f1':LastKeyCode,'f2':DiffValue,'f3':ImeOpenStatus,'f4':OldValue,'f5': NewValue,'f6': TsfMode})
 
42
                if LastKeyCode in (winUser.VK_SPACE,
 
43
                        winUser.VK_CONVERT,
 
44
                        winUser.VK_UP,
 
45
                        winUser.VK_DOWN,
 
46
                        winUser.VK_F6,
 
47
                        winUser.VK_F7,
 
48
                        winUser.VK_F8,
 
49
                        winUser.VK_F9,
 
50
                        winUser.VK_F10):
 
51
                        for i in DiffValue:
 
52
                                if nvdajp_dic.dic1.has_key(i) & (config.conf["keyboard"]["nvdajp2Key"]==True):
 
53
                                        if (nvdajp_dic.dic1.get(i)[0]==3) + (config.conf["keyboard"]["nvdajp3Key"]==True):
 
54
                                                queueHandler.queueFunction(queueHandler.eventQueue,ui.message,nvdajp_dic.dic1.get(i)[2]) # 詳細、フォネティック読み
 
55
                                        else:
 
56
                                                queueHandler.queueFunction(queueHandler.eventQueue,ui.message,i)
 
57
                                elif nvdajp_dic.dic1.has_key(i):
 
58
                                        if (nvdajp_dic.dic1.get(i)[0]!=3) & (config.conf["keyboard"]["nvdajp3Key"]==True):
 
59
                                                queueHandler.queueFunction(queueHandler.eventQueue,ui.message,nvdajp_dic.dic1.get(i)[2]) # 詳細、フォネティック読み
 
60
                                        else:
 
61
                                                queueHandler.queueFunction(queueHandler.eventQueue,ui.message,i)
 
62
                                else:
 
63
                                        queueHandler.queueFunction(queueHandler.eventQueue,ui.message,i)
 
64
                elif LastKeyCode in (winUser.VK_BACK,winUser.VK_DELETE):
 
65
                        queueHandler.queueFunction(queueHandler.eventQueue,ui.message, c_wchar_p(OldValue[-1]).value)
 
66
                else:
 
67
                        #if DiffValue == u'ー': DiffValue = u'チョーオン'
 
68
                        if nvdajp_dic.dic1.has_key(DiffValue): DiffValue = nvdajp_dic.dic1.get(DiffValue)[4]
 
69
                        queueHandler.queueFunction(queueHandler.eventQueue,ui.message,c_wchar_p(DiffValue).value)
 
70
        elif (LastKeyCode==winUser.VK_RETURN) & (c_wchar_p(OldValue).value!="") & (config.conf["keyboard"]["nvdajp1Key"]==True):
 
71
                        queueHandler.queueFunction(queueHandler.eventQueue,ui.message,c_wchar_p(OldValue).value)
 
72
        ## Masataka.Shinke 2011-07-10
 
73
        elif (LastKeyCode in (242,244,25)) & (ImeOpenStatus==1) & (_last_ime_status==0):
 
74
                queueHandler.queueFunction(queueHandler.eventQueue,ui.message,u"にほんごオン")
 
75
                _last_ime_status = 1
 
76
        elif (LastKeyCode in (242,243,25)) & (ImeOpenStatus==0) & (_last_ime_status==1):
 
77
                queueHandler.queueFunction(queueHandler.eventQueue,ui.message,u"にほんごオフ")
 
78
                _last_ime_status = 0
 
79
 
 
80
 
 
81
 
 
82
 
 
83
 
 
84
 
 
85
 
 
86
class nvdajpimeLoader64(object):
 
87
 
 
88
        def __init__(self):
 
89
                # Create a pipe so we can write to stdin of the loader process.
 
90
                pipeReadOrig, self._pipeWrite = winKernel.CreatePipe(None, 0)
 
91
                # Make the read end of the pipe inheritable.
 
92
                pipeRead = self._duplicateAsInheritable(pipeReadOrig)
 
93
                winKernel.closeHandle(pipeReadOrig)
 
94
                # stdout/stderr of the loader process should go to nul.
 
95
                with file("nul", "w") as nul:
 
96
                        nulHandle = self._duplicateAsInheritable(msvcrt.get_osfhandle(nul.fileno()))
 
97
                # Set the process to start with the appropriate std* handles.
 
98
                si = winKernel.STARTUPINFO(dwFlags=winKernel.STARTF_USESTDHANDLES, hSTDInput=pipeRead, hSTDOutput=nulHandle, hSTDError=nulHandle)
 
99
                pi = winKernel.PROCESS_INFORMATION()
 
100
                # Even if we have uiAccess privileges, they will not be inherited by default.
 
101
                # Therefore, explicitly specify our own process token, which causes them to be inherited.
 
102
                token = winKernel.OpenProcessToken(winKernel.GetCurrentProcess(), winKernel.MAXIMUM_ALLOWED)
 
103
                try:
 
104
                        winKernel.CreateProcessAsUser(token, None, u"lib64/nvdajpimeLoader.exe", None, None, True, None, None, None, si, pi)
 
105
                        # We don't need the thread handle.
 
106
                        winKernel.closeHandle(pi.hThread)
 
107
                        self._process = pi.hProcess
 
108
                except:
 
109
                        winKernel.closeHandle(self._pipeWrite)
 
110
                        raise
 
111
                finally:
 
112
                        winKernel.closeHandle(pipeRead)
 
113
                        winKernel.closeHandle(token)
 
114
 
 
115
        def _duplicateAsInheritable(self, handle):
 
116
                curProc = winKernel.GetCurrentProcess()
 
117
                return winKernel.DuplicateHandle(curProc, handle, curProc, 0, True, winKernel.DUPLICATE_SAME_ACCESS)
 
118
 
 
119
        def terminate(self):
 
120
                # Closing the write end of the pipe will cause EOF for the waiting loader process, which will then exit gracefully.
 
121
                winKernel.closeHandle(self._pipeWrite)
 
122
                # Wait until it's dead.
 
123
                winKernel.waitForSingleObject(self._process, winKernel.INFINITE)
 
124
                winKernel.closeHandle(self._process)
 
125
 
 
126
                
 
127
#
 
128
def internal_keyDownEvent(vkCode,scanCode,extended,injected):
 
129
        try:
 
130
                global RunFlag
 
131
                if vkCode in [winUser.VK_ESCAPE]:
 
132
                        RunFlag = False
 
133
                print(vkCode)
 
134
                return True
 
135
        except:
 
136
                return True
 
137
#
 
138
def internal_keyUpEvent(vkCode,scanCode,extended,injected):
 
139
        try:
 
140
                return True
 
141
        except:
 
142
                return True
 
143
                
 
144
def initialize():
 
145
        try:
 
146
                global  _nvdajpimeLoader64
 
147
                if os.environ.get('PROCESSOR_ARCHITEW6432')=='AMD64':
 
148
                        _nvdajpimeLoader64=nvdajpimeLoader64()
 
149
        except:
 
150
                pass
 
151
                
 
152
def terminate():
 
153
        try:
 
154
                global  _nvdajpimeLoader64
 
155
                if _nvdajpimeLoader64:
 
156
                        _nvdajpimeLoader64.terminate()
 
157
                        _nvdajpimeLoader64=None
 
158
        except:
 
159
                pass
 
160
                
 
161
if __name__ == '__main__':
 
162
        initialize()
 
163
        winInputHook.initialize()
 
164
        winInputHook.setCallbacks(keyDown=internal_keyDownEvent,keyUp=internal_keyUpEvent)
 
165
        while RunFlag:
 
166
                pythoncom.PumpWaitingMessages()
 
167
        winInputHook.terminate()
 
168
        terminate()
 
169
 
 
170
                
 
171
                
 
 
b'\\ No newline at end of file'