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

Bug: Using AHI the enter key for the kanata passthrou dll does not work and also having some tweaking questions #1234

Open
1 task done
pro470 opened this issue Sep 11, 2024 · 0 comments
Assignees
Labels
ahk Relates to AHK integration. jtroo does not maintain AHK functionally. bug Something isn't working windows Issue pertains to Windows only

Comments

@pro470
Copy link

pro470 commented Sep 11, 2024

Requirements

Describe the bug

I created my AHI script to have an Interception driver for my keyboard still because I use the Kanata interception version right now. So I tried it. It works but the enter button doesn't work. It doesn't have to do with the output because I assigned k with ret and it works. So there is a problem with the input. For more context, it is also not AHI because when you run the Monitor.ahk script you see everything is working fine. The issue relies on the input for the Kanata dll.

Furthermore, I have some tweaking questions.

  1. how hard would it be to also have a TCP server feature? I saw in the source code that there was a comment saying any need in a DLL and the answer is yes because I have an application that relies on it like Komokana for automatic layer switching. The only way to abandon the TCP feature is to have a connection between ahk and the DLL and also send the JSON that the TCP server sends so we can use the ahk to create the TCP or named pipe and give the people on how they want to connect other applications. Also, the information could lead to more configuration on the Autohotkey implementation letting certain keys have different actions in different layers.
  2. Is it possible to have emoji support here me out on that one the reason why I want that is so I can assign specific ahk functions to certain keys because I need unique keys for that so the only thing I want is to just send the Unicode just like sending the scancode or the virtual key. The ahk function is for my window manager komorebi primarily.

Relevant kanata config

I use the kanata_dll.kbd confing for the testing:

;;Test config for kanata.dll use by AutoHotkey, only maps two keys (f,j) to left/right modtap home row mod Shifts
(defcfg
  process-unmapped-keys	yes	;;|no| enable processing of keys that are not in defsrc
  log-layer-changes    	no 	;;|no| overhead
)

(defvar
  🕐↕	1000 ;;|500| tap-timeout
  🕐🠿	1500 ;;|500| hold-timeout
  )
(defalias	;;      timeout→ 	tap	  hold   ¦	 tap	 hold ←action
  f⌂‹⇧   	(tap-hold-release	$🕐↕	$🕐🠿       	f   	‹⇧)
  j⌂⇧›   	(tap-hold-release	$🕐↕	$🕐🠿       	j   	⇧›)
   😀  (🔣 😀)
  )

(defsrc      f     j   k )
(deflayer ⌂ @f⌂‹⇧ @j⌂⇧›  ret)

To Reproduce

  1. you need to set the AHI workstation up which is described better here https://github.com/evilC/AutoHotInterception?tab=readme-ov-file#build-your-autohotinterception-folder
  2. then take my script down below and put it in the folder where also the monitor.ahk is.
  3. also don't forget to put the kanata_passthru.dll in there
  4. then just run the script

Expected behavior

because of the process-unmapped-keys the enter key should work like a normal enter key

Kanata version

1.7.0-prerelease-1

Debug logs

No response

Operating system

Windows 11

Additional context

here is my AHI script:

#SingleInstance force
Persistent
#include Lib\AutoHotInterception.ahk
#Requires AutoHotkey v2.0

  libPath   	:= "./"              	; kanata_passthru.dll @ this folder
  kanata_cfg	:= "./kanata_dll.kbd"	; kanata config @ this file location
  dbg       	:= 1                 	; script's debug level (0 to silence some of its output)
  port          := "9999"


get_thread_id() {
  return DllCall("GetCurrentThreadId", "UInt")
}

lErr:=1, lWarn:=2, lInf:=3, lDbg:=4, lTrace:=5, log_lvl := lDbg ; Kanata's
id_thread := get_thread_id()
t3 := 0
enabled := true

  ; set up machinery for AHK and Kanata to communicate
libNm            	:= "kanata_passthru"
lib𝑓             	:= libNm '\' 'lib_kanata_passthru' ; receives AHK's address of AHK's cb KanataOut that accepts simulated output events
lib𝑓input_ev     	:= libNm '\' 'input_ev_listener' ; receives key input and uses event_loop's input event handler callback (which will in turn communicate via the internal kanata's channels to keyberon state machine etc.)
lib𝑓output_ev    	:= libNm '\' 'output_ev_check' ; checks if output event is ready (it's sent to our callback if it is)
lib𝑓reset        	:= libNm '\' 'reset_kanata_state' ; reset kanata's state
hModule          	:= DllCall("LoadLibrary", "Str",libPath libNm '.dll', "Ptr")  ; Avoids the need for DllCall in the loop to load the library
fnKanata_main    	:= DllCall.Bind(lib𝑓, 'Ptr',unset, 'Str',unset, 'Int',unset, 'Str',unset)
fnKanata_in_ev   	:= DllCall.Bind(lib𝑓input_ev, 'Int',unset , 'Int',unset,  'Int',unset)
K_output_ev_check	:= DllCall.Bind(lib𝑓output_ev)
fnKanata_reset   	:= DllCall.Bind(lib𝑓reset, 'Int',unset)


AHI := AutoHotInterception()

addr_cbKanataOut := CallbackCreate(cbKanataOut)

fnKanata_main(addr_cbKanataOut,kanata_cfg,log_lvl, port) ; setup kanata, passign ahk callback to accept out key events

preciseTΔ(n:=3) {
	static start := nativeFunc.GetSystemTimePreciseAsFileTime()
	t := round(     nativeFunc.GetSystemTimePreciseAsFileTime() - start,n)
	return t
  }
  
  class nativeFunc {
	static GetSystemTimePreciseAsFileTime() {
	  /* learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtimepreciseasfiletime
	  retrieves the current system date and time with the highest possible level of precision (<1us)
	  FILETIME structure contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC)
	  100 ns  ->  0.1 µs  ->  0.001 ms  ->  0.00001 s
	  1     sec  ->  1000 ms  ->  1000000 µs
	  0.1   sec  ->   100 ms  ->   100000 µs
	  0.001 sec  ->    10 ms  ->    10000 µs
	  */
	  static interval2sec := (10 * 1000 * 1000) ; 100ns * 10 → µs * 1000 → ms * 1000 → sec
	  DllCall("GetSystemTimePreciseAsFileTime", "int64*",&ft:=0)
	  return ft / interval2sec
	}
  }
  
keyboardId := AHI.GetKeyboardId(0x3151, 0x4002)
AHI.SubscribeKeyboard(keyboardId, true, KeyEvent)
return

KeyEvent(code, state){
	scancode := Format("{:X}", code)
	;ToolTip(scancode)
	vk := GetKeyVK("SC" scancode)
	;ToolTip(Format("{:x}", vk))
	if state == 1 {
		state := 0
	} else {
		state := 1
	}
	isH := fnKanata_in_ev(vk,code,state)

	loop 10 { ; get the remaining out keys from kanata
		isOut := K_output_ev_check()
		if (isOut < 0) {
		  break
		}
	  }  
  
}
cbKanataOut(kvk, ksc, up) {
	global t3, AHI, keyboardId
	ToolTip(Format("{:x}", kvk))
	t1 := preciseTΔ()
	vk_hex := Format("vk{:x}", kvk)
    if isSet(vk_hex) {
		if up {
		  ; SendEvent('{' vk_hex ' up}')
		  SendEvent('{' vk_hex ' up}')
		} else {
		  ; SendEvent('{' vk_hex ' down}')
		  SendEvent('{' vk_hex ' down}')
		}
	  }
	  t2 := preciseTΔ(), t3 := t2-t1
}

+Esc::
{
    global enabled
    enabled := !enabled
    AHI.SetState(enabled)
}


#Esc::
{
	global t3
	res := fnKanata_reset(t3) ; reset kanata's state, progressing time to catch up, release held keys (even those physically held sinc reset is reset, so from kanata's perspective they should be released)
	loop 10 { ; get the remaining out keys from kanata
		isOut := K_output_ev_check()
		if (isOut < 0) {
		  break
		}
	  }  
	hModule:=0
	ExitApp
}

@pro470 pro470 added the bug Something isn't working label Sep 11, 2024
@jtroo jtroo added windows Issue pertains to Windows only PRs welcome jtroo has no plans to work on this at present, but PRs are welcome ahk Relates to AHK integration. jtroo does not maintain AHK functionally. and removed PRs welcome jtroo has no plans to work on this at present, but PRs are welcome labels Sep 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ahk Relates to AHK integration. jtroo does not maintain AHK functionally. bug Something isn't working windows Issue pertains to Windows only
Projects
None yet
Development

No branches or pull requests

2 participants