Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using imm.h relate api to enable switching input mode inside IME #57

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ Click [here](https://github.com/daipeihust/im-select/blob/8080ad18f20218d1b6b5ef

Download the [im-select.exe](https://github.com/daipeihust/im-select/raw/master/win/out/x86/im-select.exe), and move it to the proper path.(If you need the 64 bit version, you can download [this one](https://github.com/daipeihust/im-select/raw/master/im-select-win/out/x64/im-select.exe).)

Also see [select-im-imm](./win-imm/im-select-imm//README.md)
Which provide ability for switch IME Mode.

### linux

You don't have to install this for linux. linux have tools to switch input methods
Expand Down
2 changes: 2 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ Put `gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell
```

> 注意:Windows系统的路径和Linux会有些不一样,类似这样: C:\Users\path\to\file
### windows-imm
详见 [select-im-imm](./win-imm/im-select-imm/README_CN.md) 提供切换同一输入法下中英文的能力

# 联系我

Expand Down
8 changes: 8 additions & 0 deletions win-imm/im-select-imm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Xmake cache
.xmake/
build/

# MacOS Cache
.DS_Store
.vscode/

35 changes: 35 additions & 0 deletions win-imm/im-select-imm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Im-Select-Imm

## Introduce
Orgin im-select could only change IME
This version allow you change IME Mode in second parameter
The pre-build exe is under the `/win-imm/out/`

## Usage

## Get Current IME key

```shell
/path/to/im-select-imm.exe
Out: [current IME] [current Mode]
```

## Switch IME

```shell
/path/to/im-select-imm.exe [target IME]
Or
/path/to/im-select-imm.exe [target IME] [target IME Mode]
```

## For Microsoft Chinese IME
```
For Microsoft Old Chinese IME(Win10 and Previous) :
0: English
1: Chinese
For Microsoft New Chinese IME(Win11) :
0: English / Half Shape
1: Chinese / Half Shape
1024: English / Full Shape
1025: Chinese / Full Shape
```
38 changes: 38 additions & 0 deletions win-imm/im-select-imm/README_CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Im-Select-Imm

## 介绍

原版im-select可以切换输入法, 但是不能切换中英文。
这个版本可以在第二个参数中切换中英文和全角半角。
二进制位于 `/win-imm/out/` 中

## 使用方式

### 下载

### 获取当前输入法的key

```shell
/path/to/im-select-imm.exe
Out: [当前输入法] [输入法的当前模式]
```

### 切换输入法

```shell
/path/to/im-select.exe [目标输入法]
Or
/path/to/im-select.exe [目标输入法] [目标输入法的目标模式]
```

### 对于微软输入法
```
For Microsoft Old Chinese IME(Win10 and Previous) :
0: English
1: Chinese
For Microsoft New Chinese IME(Win11) :
0: English / Half Shape
1: Chinese / Half Shape
1024: English / Full Shape
1025: Chinese / Full Shape
```
Binary file added win-imm/im-select-imm/inc/stdafx.h
Binary file not shown.
Binary file added win-imm/im-select-imm/inc/targetver.h
Binary file not shown.
70 changes: 70 additions & 0 deletions win-imm/im-select-imm/src/im-select-imm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "stdafx.h"
#include <cstdlib>

#include <Windows.h>
#include <immdev.h>

using namespace std;

int getInputMethod() {
HWND hwnd = GetForegroundWindow(); //dll
if (hwnd) {
DWORD threadID = GetWindowThreadProcessId(hwnd, NULL); //dll
HKL currentLayout = GetKeyboardLayout(threadID); //dll
unsigned int x = (unsigned int)currentLayout & 0x0000FFFF;
return ((int)x);
}
return 0;
}
void switchInputMethod(int locale) {
HWND hwnd = GetForegroundWindow(); //dll
LPARAM currentLayout = ((LPARAM)locale);
PostMessage(hwnd, WM_INPUTLANGCHANGEREQUEST, 0, currentLayout); //dll
}

// API: https://learn.microsoft.com/en-us/previous-versions/aa913780(v=msdn.10)
// For Microsoft Old Chinese IME(Win10 and Previous) :
// 0: English
// 1: Chinese
// For Microsoft New Chinese IME(Win11) :
// 0: English / Half Shape
// 1: Chinese / Half Shape
// 1024: English / Full Shape (Bit10 and Bit1 used)
// 1025: Chinese / Full Shape
LRESULT getInputMode(){
HWND foregroundWindow = GetForegroundWindow();
HWND foregroundIME = ImmGetDefaultIMEWnd(foregroundWindow);
if(foregroundIME){
LRESULT result = SendMessage(foregroundIME, WM_IME_CONTROL, 0x001, 0);
return result;
} else {
return 0;
}
}

void switchInputMode(LRESULT locale_mode){
HWND foregroundWindow = GetForegroundWindow();
HWND foregroundIME = ImmGetDefaultIMEWnd(foregroundWindow);
LPARAM currentMode = (LPARAM)locale_mode;
SendMessage(foregroundIME, WM_IME_CONTROL, IMC_SETCONVERSIONMODE, currentMode);
}


int main(int argc, char** argv)
{
if (argc == 1) {
int imID = getInputMethod();
int imMode = getInputMode();
printf("%d %d\n", imID,imMode);
} else if( argc == 2 ) {
int locale = atoi(argv[1]);
switchInputMethod(locale);
} else {
int locale = atoi(argv[1]);
LRESULT locale_mode = atoi(argv[2]);
switchInputMethod(locale);
switchInputMode(locale_mode);
}
return 0;
}

Binary file added win-imm/im-select-imm/src/stdafx.cpp
Binary file not shown.
85 changes: 85 additions & 0 deletions win-imm/im-select-imm/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
add_rules("mode.debug", "mode.release")


target("im-select-imm")
set_kind("binary")
add_files("src/*.cpp")
add_files("src/stdafx.cpp")

add_includedirs("inc/")

add_rules("win.sdk.application")
add_ldflags("-subsystem:console")
add_links("imm32")




-- If you want to known more usage about xmake, please see https://xmake.io
--
-- ## FAQ
--
-- You can enter the project directory firstly before building project.
--
-- $ cd projectdir
--
-- 1. How to build project?
--
-- $ xmake
--
-- 2. How to configure project?
--
-- $ xmake f -p [macosx|linux|iphoneos ..] -a [x86_64|i386|arm64 ..] -m [debug|release]
--
-- 3. Where is the build output directory?
--
-- The default output directory is `./build` and you can configure the output directory.
--
-- $ xmake f -o outputdir
-- $ xmake
--
-- 4. How to run and debug target after building project?
--
-- $ xmake run [targetname]
-- $ xmake run -d [targetname]
--
-- 5. How to install target to the system directory or other output directory?
--
-- $ xmake install
-- $ xmake install -o installdir
--
-- 6. Add some frequently-used compilation flags in xmake.lua
--
-- @code
-- -- add debug and release modes
-- add_rules("mode.debug", "mode.release")
--
-- -- add macro defination
-- add_defines("NDEBUG", "_GNU_SOURCE=1")
--
-- -- set warning all as error
-- set_warnings("all", "error")
--
-- -- set language: c99, c++11
-- set_languages("c99", "c++11")
--
-- -- set optimization: none, faster, fastest, smallest
-- set_optimize("fastest")
--
-- -- add include search directories
-- add_includedirs("/usr/include", "/usr/local/include")
--
-- -- add link libraries and search directories
-- add_links("tbox")
-- add_linkdirs("/usr/local/lib", "/usr/lib")
--
-- -- add system link libraries
-- add_syslinks("z", "pthread")
--
-- -- add compilation and link flags
-- add_cxflags("-stdnolib", "-fno-strict-aliasing")
-- add_ldflags("-L/usr/local/lib", "-lpthread", {force = true})
--
-- @endcode
--

Binary file added win-imm/out/im-select-imm.exe
Binary file not shown.