Hey, I have put together a script that uses kport to send keyboard commands.
I must caution anyone who uses kport, the first reboot after installing it I had BIOS problems and couldn't boot. It said that I had an invalid configuration. I solved it by pulling the BIOS battery and resetting the BIOS clock... so if you also run into this problem, at least you have one solution to try.
You can send an entire string or individual keypresses and releases. Should work for most strings, although I do not have it parse the string for special characters... so it will send LITERALS. ie. # is the pound sign NOT the windows key.
Also, I haven't quite worked out the timing issues. Occasionally (about 1 in 52) it will miss a keypress for some reason.
Okay, warnings aside here is the code:
Code:
Dll_Path := "PathTo_KPort.dll" ;You have to adjust the Dll_Path variable.
;==========================
; KEYBOARD |
;==========================
;===========================
;===========================
Send_String_KPort(String, Direct = FALSE, SuspendWhileSending = TRUE)
{
Capitals := "ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()<>?:{}|~+_" . Chr(34)
Loop, Parse, String
{
if ( (InStr( Capitals, A_LoopField, TRUE) = 0 )) {
Send_KPort(A_LoopField)
} else {
Send_KPort_Press("Shift")
sleep 10
Send_KPort(A_LoopField)
sleep 10
Send_KPort_Release("Shift")
}
}
}
return
;===========================
;===========================
Send_KPort_Press(key, Direct = FALSE, SuspendWhileSending = TRUE )
{ global Dll_Path
if (SuspendWhileSending) {
Suspend, On
}
if (Direct) {
SCAN_CODE := key
} else {
SCAN_CODE := GetScanCode(key)
}
Kport_Wait(1)
DllCall(Dll_Path . "Kport\_Outportb@8", Short, 0x64, Char, 0xD2)
Kport_Wait(1)
DllCall(Dll_Path . "Kport\_Outportb@8", Short, 0x60, Char, SCAN_CODE)
Suspend, Off
}
return
;===========================
;===========================
Send_KPort_Release(key, Direct = FALSE, SuspendWhileSending = TRUE)
{
if (SuspendWhileSending) {
Suspend, On
}
if (Direct) {
SCAN_CODE := key
} else {
SCAN_CODE := GetScanCode(key)
}
SetFormat Integer, H
SCAN_CODE += 0x80
Send_KPort_Press(SCAN_CODE, TRUE, SuspendWhileSending)
}
return
;===========================
;===========================
Send_KPort(key, Direct = FALSE, SuspendWhileSending = TRUE)
{
Send_KPort_Press(key, Direct, SuspendWhileSending)
Send_KPort_Release(key, Direct, SuspendWhileSending)
}
return
;===========================
;===========================
GetScanCode(key)
{
if (key = "!")
return 0x02
if (key = "@" )
return 0x03
if (key = "#" )
return 0x04
if (key = "$" )
return 0x05
if (key = "%" )
return 0x06
if (key = "^" )
return 0x07
if (key = "&" )
return 0x08
if (key = "*" )
return 0x09
if (key = "(" )
return 0x0a
if (key = ")" )
return 0x0b
if (key = "-" )
return 0x0c
if ( (key = "+" ) OR (key = "=") )
return 0x0d
if ( (key = "[") OR (key = "{") )
return 0x1a
if ( (key = "]") OR (key = "}") )
return 0x1b
if ( (key = ";") OR (key = ":") )
return 0x27
if ( (key = "'") OR (key = Chr(34)) )
return 0x28
if ( (key = "`") OR (key = "~") )
return 0x29
if ( (key = "\") OR (key = "|") )
return 0x2b
if ( (key = ",") OR (key = "<") )
return 0x33
if ( (key = ".") OR (key = ">") )
return 0x34
if ( (key = "/") OR (key = "?") )
return 0x35
if ( (key = " ") OR (key = A_Space) )
return 0x39
ScanCode_1 := 0x02
ScanCode_2 := 0x03
ScanCode_3 := 0x04
ScanCode_4 := 0x05
ScanCode_5 := 0x06
ScanCode_6 := 0x07
ScanCode_7 := 0x08
ScanCode_8 := 0x09
ScanCode_9 := 0x0a
ScanCode_0 := 0x0b
ScanCode__ := 0x0c
ScanCode_q := 0x10
ScanCode_w := 0x11
ScanCode_e := 0x12
ScanCode_r := 0x13
ScanCode_t := 0x14
ScanCode_y := 0x15
ScanCode_u := 0x16
ScanCode_i := 0x17
ScanCode_o := 0x18
ScanCode_p := 0x19
ScanCode_a := 0x1e
ScanCode_s := 0x1f
ScanCode_d := 0x20
ScanCode_f := 0x21
ScanCode_g := 0x22
ScanCode_h := 0x23
ScanCode_j := 0x24
ScanCode_k := 0x25
ScanCode_l := 0x26
ScanCode_z := 0x2c
ScanCode_x := 0x2d
ScanCode_c := 0x2e
ScanCode_v := 0x2f
ScanCode_b := 0x30
ScanCode_n := 0x31
ScanCode_m := 0x32
ScanCode_F1 := 0x3b
ScanCode_F2 := 0x3c
ScanCode_F3 := 0x3d
ScanCode_F4 := 0x3e
ScanCode_F5 := 0x3f
ScanCode_F6 := 0x40
ScanCode_F7 := 0x41
ScanCode_F8 := 0x42
ScanCode_F9 := 0x43
ScanCode_F10 := 0x44
ScanCode_F11 := 0x57
ScanCode_F12 := 0x58
ScanCode_Esc := 0x00
ScanCode_Backspace := 0x0e
ScanCode_Tab := 0x0f
ScanCode_Enter := 0x1c
ScanCode_Control := 0x1d
ScanCode_LCtrl := 0x1d
ScanCode_Shift := 0x2a
ScanCode_LShift := 0x2a
ScanCode_RShift := 0x36
ScanCode_Alt := 0x38
ScanCode_LAlt := 0x38
ScanCode_Space := 0X39
ScanCode_CapsLock := 0x3a
ScanCode_NumLock := 0x45
ScanCode_ScrollLock := 0x46
ScanCode_NumPad7 := 0x47
ScanCode_NumPadHome := 0x47
ScanCode_NumPad8 := 0x48
ScanCode_NumPadUp := 0x48
ScanCode_NumPad9 := 0x49
ScanCode_NumPadPgUp := 0x49
ScanCode_NumPad4 := 0x4b
ScanCode_NumPadLeft := 0x4b
ScanCode_NumPad5 := 0x4c
ScanCode_NumPad6 := 0x4d
ScanCode_NumPadRight := 0x4d
ScanCode_NumPad1 := 0x4f
ScanCode_NumPadEnd := 0x4f
ScanCode_NumPad2 := 0x50
ScanCode_NumPadDown := 0x50
ScanCode_NumPad3 := 0x51
ScanCode_NumPadPgDn := 0x51
ScanCode_NumPad0 := 0x52
ScanCode_NumPadIns := 0x52
ScanCode_NumPadDel := 0x53
if (ScanCode_%key%) {
return ScanCode_%key%
}
}
return
;===========================
; UTILITIES |
;===========================
;===========================
;===========================
Num2Bin(Number)
{
Number := Abs(Number)
while (Number <> 0) {
Digit := Floor(Mod(Number, (2**(A_Index))))
Number += -Digit
Digit := Floor(Digit / (2**(A_Index - 1)))
ReverseBin .= Digit
}
Loop, Parse, ReverseBin
{
Bin .= SubStr(ReverseBin, -(A_Index - 1) , 1)
}
return Bin
}
return
;===========================
;===========================
Num22Compliment(Number, Size=8)
{
if (Number >= 0) {
while (Number <> 0) {
Digit := Floor(Mod(Number, (2**(A_Index))))
Number += -Digit
Digit := Floor(Digit / (2**(A_Index - 1)))
ReverseBin .= Digit
}
while (StrLen(ReverseBin) < Size) {
ReverseBin .= "0"
}
Loop, Parse, ReverseBin
{
Bin .= SubStr(ReverseBin, -(A_Index - 1) , 1)
}
return Bin
} else {
Num := Num22Compliment(-Number)
Loop, Parse, Num
{
if (A_LoopField = 1) {
Bin .= "0"
} else {
Bin .= "1"
}
}
Bin := Num2Bin(Bin2Num(Bin) + 1)
return Bin
}
}
return
;===========================
;===========================
Num2Hex(Number)
{
Number := Abs(Number)
while (Number <> 0) {
Digit := Floor(Mod(Number, (16**(A_Index))))
Number += -Digit
Digit := Floor(Digit / (16**(A_Index - 1)))
if (Digit = 10)
Digit = a
if (Digit = 11)
Digit = b
if (Digit = 12)
Digit = c
if (Digit = 13)
Digit = d
if (Digit = 14)
Digit = e
if (Digit = 15)
Digit = f
ReverseHex .= Digit
}
Loop, Parse, ReverseHex
{
Bin .= SubStr(ReverseHex, -(A_Index - 1) , 1)
}
return Bin
}
return
;===========================
;===========================
Bin2Num(Binary)
{
Loop, Parse, Binary
{
Number += A_LoopField * 2**(StrLen(Binary)- A_Index)
}
return Number
}
return
;===========================
;===========================
Bin2Hex(Binary)
{
Hex := ""
ReverseHex := ""
Number := Bin2Num(Binary)
while (Number <> 0) {
Digit := Floor(Mod(Number, (16**(A_Index))))
Number += -Digit
Digit := Floor(Digit / (16**(A_Index - 1)))
if (Digit = 10)
Digit = a
if (Digit = 11)
Digit = b
if (Digit = 12)
Digit = c
if (Digit = 13)
Digit = d
if (Digit = 14)
Digit = e
if (Digit = 15)
Digit = f
ReverseHex .= Digit
}
ReverseHex .= "x0"
Loop, Parse, ReverseHex
{
Hex .= SubStr(ReverseHex, -(A_Index - 1) , 1)
}
return Hex
}
return
;===========================
;===========================
Kport_Wait(Type) {
info := 0x00
TimeOut = 10000
if( Type = 0) {
while( TimeOut > 0) {
TimeOut += -1
DllCall(Dll_Path . "Kport\_Inportb@8", Short, 0x64, Byte, &info )
if( ( info & 1) = 1) {
return
}
}
return
}
else
{
while(TimeOut > 0 ) {
TimeOut += -1
DllCall(Dll_Path . "Kport\_Inportb@8", Short, 0x64, Byte, &info )
if((info & 2) = 0) {
return
}
}
return
}
}