AutoHotkey Community

It is currently May 27th, 2012, 12:15 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: emacs-key-sequence.ahk
PostPosted: May 31st, 2011, 9:58 am 
Offline

Joined: May 16th, 2011, 2:28 am
Posts: 43
Location: China
code is host on github.com ,you can find it
here: https://github.com/jixiuf/my_autohotkey ... hk_scripts
Code:
#Include emacs-key-sequence.ahk

$^x::
map:=Object()
map.insert("^x^f","msg1")
map.insert("^x^e^e","msg2")
map.insert("^xvv","msg3")
prefixKey("^x",map)
return



msg1(){
  MsgBox ,this is function msg1(),will be called when you press ^x^f in this example
}
msg2(){
  MsgBox ,this is function msg2(),will be called when you press ^x^e^e in this example
}
msg3(){
  MsgBox ,this is function msg3(),will be called when you press ^xvv in this example
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;another one

$#x::
map2:=Object()
map2.insert("#x#s" ,"msg5")
map2.insert("#x#f" ,"msg4")
map2.insert("#x{F1}" ,"msg6")
prefixKey("#x", map2)
return

msg4(){
MsgBox ,msg4 is called when you press "Win+x Win+f"
}

msg5(){
MsgBox ,msg5 is called  when you press "Win+x Win+s"
}
msg6(){
MsgBox ,msg6 is called  when you press "Win+x F1"
}



Code:
;;emacs-key-sequence.ahk --   binding GNU/Emacs like key
       
;; about how to use this emacs-key-sequence.ahk
;; 1. include emacs-key-sequence.ahk in you ahk files
;; like this :
;; #Include emacs-key-sequence.ahk
;; 2. define a Object() ,a map
;;   and insert("keys" ,"functionNameYouWantToCallWhenYouPresskeys")
;; for example ,
;; map:=Object()
;; map.insert("^x^f","msg1")
;; map.insert("^x^e^e","msg2")
;; map.insert("^xvv","msg3")
;;
;; 3 ,bind  the root key prefix to the root key prefix
;; for example
;; $^x::
;;      map:=Object()
;;      map.insert("^x^f","msg1")
;;      map.insert("^x^e^e","msg2")
;;      map.insert("^xvv","msg3")
;;    prefixKey("^x",map)
;; return
;; $^d::
;;   map:=Object()
;;      map.insert("^d^f","msg1")
;;      map.insert("^d^e^e","msg2")
;;      map.insert("^dvv","msg3")
;;    prefixKey("^d",map)
;; the "$" ,must be used

;;4 define functions that will be called the some key is pressed
;; msg1(){
;;   MsgBox ,this is function msg1(),will be called when you press ^x^f in this example
;; }
;; msg2(){
;;   MsgBox ,this is function msg2(),will be called when you press ^x^e^e in this example
;; }
;; msg3(){
;;   MsgBox ,this is function msg3(),will be called when you press ^xvv in this example
;; }



setkeydelay 0

prefixKey(prefix ,keyFuncMap){
seq:=prefix
loop,
{
; {LCsontrol}{RControl}{LAlt}{RAlt}{LShift}{RShift}{LWin}{RWin}{AppsKey}{F1}{F2}{F3}{F4}{F5}{F6}{F7}{F8}{F9}{F10}{F11}{F12}{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn}{Del}{Ins}{BS}{Capslock}{Numlock}{PrintScreen}{Pause}{Escape}
Input,input,L1,{Escape}{LControl}{RControl}{LAlt}{RAlt}{LShift}{RShift}{LWin}{RWin}abcdefghijklmnopqrstuvwxyz1234567890{F1}{F2}{F3}{F4}{F5}{F6}{F7}{F8}{F9}{F10}{F11}{F12}{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn}{Del}{Ins}{BS}{Capslock}{Numlock}{PrintScreen}{Pause}{Escape}
    if ErrorLevel = EndKey:Escape
    {
     seq =prefix
    }
if ErrorLevel = EndKey:LControl 
{
continue
}
if ErrorLevel = EndKey:RControl 
{
continue
}
if ErrorLevel = EndKey:LWin 
{
continue
}
if ErrorLevel = EndKey:RWin 
{
continue
}
if ErrorLevel = EndKey:LAlt 
{
continue
}
if ErrorLevel = EndKey:RAlt 
{
continue
}
if ErrorLevel = EndKey:LShift 
{
continue
}
if ErrorLevel = EndKey:RShift 
{
continue
}
if ErrorLevel = EndKey:a 
{
input=a
}
if ErrorLevel = EndKey:b 
{
input=b
}
if ErrorLevel = EndKey:c 
{
input=c
}
if ErrorLevel = EndKey:d 
{
input=d
}
if ErrorLevel = EndKey:e 
{
input=e
}
if ErrorLevel = EndKey:f 
{
input=f
}
if ErrorLevel = EndKey:g 
{
input=g
}
if ErrorLevel = EndKey:h 
{
input=h
}
if ErrorLevel = EndKey:i 
{
input=i
}
if ErrorLevel = EndKey:j 
{
input=j
}
if ErrorLevel = EndKey:k 
{
input=k
}
if ErrorLevel = EndKey:l 
{
input=l
}
if ErrorLevel = EndKey:m 
{
input=m
}
if ErrorLevel = EndKey:n 
{
input=n
}
if ErrorLevel = EndKey:o 
{
input=o
}
if ErrorLevel = EndKey:p 
{
input=p
}
if ErrorLevel = EndKey:q 
{
input=q
}
if ErrorLevel = EndKey:r 
{
input=r
}
if ErrorLevel = EndKey:s 
{
input=s
}
if ErrorLevel = EndKey:t 
{
input=t
}
if ErrorLevel = EndKey:u 
{
input=u
}
if ErrorLevel = EndKey:v 
{
input=v
}
if ErrorLevel = EndKey:w 
{
input=w
}
if ErrorLevel = EndKey:x 
{
input=x
}
if ErrorLevel = EndKey:y 
{
input=y
}
if ErrorLevel = EndKey:z 
{
input=z
}
if ErrorLevel = EndKey:1 
{
input=1
}
if ErrorLevel = EndKey:2 
{
input=2
}
if ErrorLevel = EndKey:3 
{
input=3
}
if ErrorLevel = EndKey:4 
{
input=4
}
if ErrorLevel = EndKey:5 
{
input=5
}
if ErrorLevel = EndKey:6 
{
input=6
}
if ErrorLevel = EndKey:7 
{
input=7
}
if ErrorLevel = EndKey:8 
{
input=8
}
if ErrorLevel = EndKey:9 
{
input=9
}
if ErrorLevel = EndKey:0 
{
input=0
}
if ErrorLevel = EndKey:F1 
{
input={F1}
}
if ErrorLevel = EndKey:F2 
{
input={F2}
}
if ErrorLevel = EndKey:F3 
{
input={F3}
}
if ErrorLevel = EndKey:F4 
{
input={F4}
}
if ErrorLevel = EndKey:F5 
{
input={F5}
}
if ErrorLevel = EndKey:F6 
{
input={F6}
}
if ErrorLevel = EndKey:F7 
{
input={F7}
}
if ErrorLevel = EndKey:F8 
{
input={F8}
}
if ErrorLevel = EndKey:F9 
{
input={F9}
}
if ErrorLevel = EndKey:F10 
{
input={F10}
}
if ErrorLevel = EndKey:F11 
{
input={F11}
}
if ErrorLevel = EndKey:F12 
{
input={F12}
}
if ErrorLevel = EndKey:Down 
{
input={Down}
}
if ErrorLevel = EndKey:Left 
{
input={Left}
}
if ErrorLevel = EndKey:Right 
{
input={Right}
}
if ErrorLevel = EndKey:Up 
{
input={Up}
}
if ErrorLevel = EndKey:Home 
{
input={Home}
}
if ErrorLevel = EndKey:End 
{
input={End}
}
if ErrorLevel = EndKey:PageUp 
{
input={PageUp}
}
if ErrorLevel = EndKey:PageDown 
{
input={PageDown}
}
if ErrorLevel = EndKey:Del 
{
input={Del}
}
if ErrorLevel = EndKey:Ins 
{
input={Ins}
}
if ErrorLevel = EndKey:BS 
{
input={BS}
}
if ErrorLevel = EndKey:Capslock 
{
input={Capslock}
}
if ErrorLevel = EndKey:Numlock 
{
input={Numlock}
}
if ErrorLevel = EndKey:PrintScreen 
{
input={PrintScreen}
}
if ErrorLevel = EndKey:Pause 
{
input={Pause}
}
if ErrorLevel = EndKey:Escape 
{
input={Escape}
}

 tmpStr=
   if (GetKeyState("RWin")=1 ||GetKeyState("LWin")=1){
     tmpStr=#
   }
  if (GetKeyState("RControl")=1 ||GetKeyState("LControl")=1 ){
      tmpStr=%tmpStr%^
   }
  if (GetKeyState("RAlt")=1 ||GetKeyState("LAlt")=1 ){
      tmpStr=%tmpStr%!
   }
  if (GetKeyState("RShift")=1 ||GetKeyState("LShift")=1 ){
      tmpStr=%tmpStr%+
   }
   tmpStr=%tmpStr%%input%
       if tmpStr=^g  ;;Ctrl+g ,cancel ,
           break
      seq=%seq%%tmpStr%
     ;; ToolTip,%seq%
      funcName:= keyFuncMap[seq]
           if funcName<>
            {
              callFuncByName(funcName)
              break
            }else{
              containPrefix=
              For k, v in keyFuncMap
              {
                StringGetPos, pos,k, %seq%
                if pos=0  ;;if k starts with seq
                {
                 containPrefix:=true
                 break
                 }
              }
              ;;if key Prefix you press don't exists in keyFuncMap ,then
              ;;send it to system
              ;;suppose the map is {"^xvd" "function1"
              ;;                    "^xc" "function2"}
              ;; and you have pressed "^xd" , there isn't a key equas "^xd"
              ;; so no function would be called
              ;; and "^xd" will be send to system
              if containPrefix=
              {
              Tooltip ,%seq%
                  sendInput, %seq%
                  break
              }else{
                 continue
              }
            }
}

}
;;end of func prefixKey(..)

;;param string , funcName is a function
;;without paramters
callFuncByName(funcName){
  fun1:=RegisterCallback(funcName, "F", 0)
  DllCall(fun1) 

}
;; key:=map["^xf"]
;; callFuncByName(key)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 13th, 2011, 1:54 pm 
Offline

Joined: June 24th, 2010, 12:18 pm
Posts: 10
hi! i'm getting
Quote:
error at line 371 in #include file "c:\program files\autohotkey\emacs-key-sequence.ahk".

Line Text: For k, v in keyFuncMap
Error: This line does not contain a recognized action

The Script was not reloaded; the old version will remain in effect.


Please help! your script is the best!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 13th, 2011, 3:05 pm 
Offline

Joined: May 16th, 2011, 2:28 am
Posts: 43
Location: China
maybe ,you need AutoHotKey_L


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 13th, 2011, 5:53 pm 
Offline

Joined: June 24th, 2010, 12:18 pm
Posts: 10
hey, i installed autohotkey_L and problem solved! thank you :D


Last edited by surajsharma on August 14th, 2011, 3:00 am, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 14th, 2011, 2:59 am 
Offline

Joined: June 24th, 2010, 12:18 pm
Posts: 10
let me just say this is an amazing script. could you add the functionality for something like this:
Code:
$^x::
map:=Object()
map.insert("^xf","msg1")
map.insert("^xfv","msg2")
prefixKey("^x",map)
return


the problem is obvious, once ^xf is detected, ^xfv can't really run :/
please add a timer or something


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 24th, 2012, 7:18 pm 
Offline

Joined: October 27th, 2006, 10:12 am
Posts: 649
Looks great, thanks.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: fincs and 7 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group