Skip to content

Commit

Permalink
feat: mdsn에 따라 nCode 값에 따른 처리 수행
Browse files Browse the repository at this point in the history
  • Loading branch information
codepage949 committed Jul 13, 2022
1 parent c4784d4 commit 4e8de86
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
66 changes: 34 additions & 32 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,44 @@ var otherPressedCount = 0
var otherPressedMap = map[uint32]bool{}

func keyboardHook(nCode int32, wParam uintptr, lParam unsafe.Pointer) uintptr {
kb := (*winapi.KBDLLHOOKSTRUCT)(lParam)
if nCode == winapi.HC_ACTION {
kb := (*winapi.KBDLLHOOKSTRUCT)(lParam)

if kb.VkCode == winapi.VK_LSHIFT {
if wParam == winapi.WM_KEYDOWN {
if otherPressedCount == 0 {
lShiftAlone = true
}
} else if wParam == winapi.WM_KEYUP {
if lShiftAlone {
// NOTE: https://stackoverflow.com/questions/64280975/immgetcontext-returns-zero-always
hwnd := winapi.ImmGetDefaultIMEWnd(winapi.GetForegroundWindow())
tid := uint32(winapi.GetWindowThreadProcessId(hwnd, 0))
lid := winapi.GetKeyboardLayout(tid)
if kb.VkCode == winapi.VK_LSHIFT {
if wParam == winapi.WM_KEYDOWN {
if otherPressedCount == 0 {
lShiftAlone = true
}
} else if wParam == winapi.WM_KEYUP {
if lShiftAlone {
// NOTE: https://stackoverflow.com/questions/64280975/immgetcontext-returns-zero-always
hwnd := winapi.ImmGetDefaultIMEWnd(winapi.GetForegroundWindow())
tid := uint32(winapi.GetWindowThreadProcessId(hwnd, 0))
lid := winapi.GetKeyboardLayout(tid)

// NOTE: https://docs.microsoft.com/ko-kr/windows/win32/api/winuser/nf-winuser-getkeyboardlayout?redirectedfrom=MSDN#return-value
// locale 식별자가 ko-KR일 경우에만 언어 변경 작업 진행
if lid&0xFFFF == 0x0412 {
// NOTE: COMPOSITIONFORM에 사용하는 CFS_EXCLUDE에 대한 참고 자료가 없으나 실제로 해보면 composition window가 나타나지 않음
cf := winapi.COMPOSITIONFORM{Style: winapi.CFS_EXCLUDE}
_ = winapi.SendMessage(hwnd, winapi.WM_IME_CONTROL, winapi.IMC_SETCOMPOSITIONWINDOW, uintptr(unsafe.Pointer(&cf)))
mode := 1 ^ winapi.SendMessage(hwnd, winapi.WM_IME_CONTROL, winapi.IMC_GETCONVERSIONMODE, 0)
_ = winapi.SendMessage(hwnd, winapi.WM_IME_CONTROL, winapi.IMC_SETCONVERSIONMODE, mode)
// NOTE: https://docs.microsoft.com/ko-kr/windows/win32/api/winuser/nf-winuser-getkeyboardlayout?redirectedfrom=MSDN#return-value
// locale 식별자가 ko-KR일 경우에만 언어 변경 작업 진행
if lid&0xFFFF == 0x0412 {
// NOTE: COMPOSITIONFORM에 사용하는 CFS_EXCLUDE에 대한 참고 자료가 없으나 실제로 해보면 composition window가 나타나지 않음
cf := winapi.COMPOSITIONFORM{Style: winapi.CFS_EXCLUDE}
_ = winapi.SendMessage(hwnd, winapi.WM_IME_CONTROL, winapi.IMC_SETCOMPOSITIONWINDOW, uintptr(unsafe.Pointer(&cf)))
mode := 1 ^ winapi.SendMessage(hwnd, winapi.WM_IME_CONTROL, winapi.IMC_GETCONVERSIONMODE, 0)
_ = winapi.SendMessage(hwnd, winapi.WM_IME_CONTROL, winapi.IMC_SETCONVERSIONMODE, mode)
}
}
}
}
} else {
if wParam&0x1 == 0 { // NOTE: WM_*DOWN
if !otherPressedMap[kb.VkCode] {
lShiftAlone = false
otherPressedCount += 1
otherPressedMap[kb.VkCode] = true
}
} else { // NOTE: WM_*UP
if otherPressedMap[kb.VkCode] {
otherPressedCount -= 1
otherPressedMap[kb.VkCode] = false
} else {
if wParam&0x1 == 0 { // NOTE: WM_*DOWN
if !otherPressedMap[kb.VkCode] {
lShiftAlone = false
otherPressedCount += 1
otherPressedMap[kb.VkCode] = true
}
} else { // NOTE: WM_*UP
if otherPressedMap[kb.VkCode] {
otherPressedCount -= 1
otherPressedMap[kb.VkCode] = false
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions winapi/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ const (
IMC_GETCONVERSIONMODE = 1
IMC_SETCONVERSIONMODE = 2
CFS_EXCLUDE = 0x80
HC_ACTION = 0
)

0 comments on commit 4e8de86

Please sign in to comment.