keybd_event

package module
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 2, 2021 License: MIT Imports: 5 Imported by: 96

README

keybd_event

go.dev reference

This library simulates the key press on a keyboard. It runs on Linux, Windows and Mac.

Important :

  • The keys change in the different keyboard layouts of the target computer(s).
  • I have tested this code on my system and I don't find any errors. If you have a bug, please create an issue.
Example :
package main

import (
	"runtime"
	"time"
	"github.com/micmonay/keybd_event"
)

func main() {
	kb, err := keybd_event.NewKeyBonding()
	if err != nil {
		panic(err)
	}

	// For linux, it is very important to wait 2 seconds
	if runtime.GOOS == "linux" {
		time.Sleep(2 * time.Second)
	}
	
	// Select keys to be pressed
	kb.SetKeys(keybd_event.VK_A, keybd_event.VK_B) 

	// Set shift to be pressed
	kb.HasSHIFT(true) 

	// Press the selected keys
	err = kb.Launching() 
	if err != nil {
		panic(err)
	}
	
	// Or you can use Press and Release
	kb.Press()
	time.Sleep(10 * time.Millisecond)
	kb.Release()

	// Here, the program will generate "ABAB" as if they were pressed on the keyboard.
}

For easy access to all the keys on the virtual keyboard, I have added more special keycodes constants VK_SP*.

Below is an illustration showing the "VK_" symbols for each key on the keyboard: keyboard.png

Linux

On Linux this library uses uinput, which on the major distributions requires root permissions.

The easy solution is executing through root user (by using sudo). A worse way is by changing the executable's permissions by using chmod.

Secure Linux Example
sudo groupadd uinput
sudo usermod -a -G uinput my_username
sudo udevadm control --reload-rules
echo "SUBSYSTEM==\"misc\", KERNEL==\"uinput\", GROUP=\"uinput\", MODE=\"0660\"" | sudo tee /etc/udev/rules.d/uinput.rules
echo uinput | sudo tee /etc/modules-load.d/uinput.conf

Another subtlety on Linux: it is important after creating the keybd_event to wait 2 seconds before running first keyboard actions.

Darwin (MacOS)

This library depends on Apple's frameworks, and I did not find a solution to cross-compile from another OS to MacOS.

Documentation

Overview

Package keybd_event is used for a key press simulated in Windows, Linux and Mac

Index

Examples

Constants

View Source
const (
	VK_SP1  = 41
	VK_SP2  = 12
	VK_SP3  = 13
	VK_SP4  = 26
	VK_SP5  = 27
	VK_SP6  = 39
	VK_SP7  = 40
	VK_SP8  = 43
	VK_SP9  = 51
	VK_SP10 = 52
	VK_SP11 = 53
	VK_SP12 = 86

	VK_ESC = 1
	VK_1   = 2
	VK_2   = 3
	VK_3   = 4
	VK_4   = 5
	VK_5   = 6
	VK_6   = 7
	VK_7   = 8
	VK_8   = 9
	VK_9   = 10
	VK_0   = 11
	VK_Q   = 16
	VK_W   = 17
	VK_E   = 18
	VK_R   = 19
	VK_T   = 20
	VK_Y   = 21
	VK_U   = 22
	VK_I   = 23
	VK_O   = 24
	VK_P   = 25
	VK_A   = 30
	VK_S   = 31
	VK_D   = 32
	VK_F   = 33
	VK_G   = 34
	VK_H   = 35
	VK_J   = 36
	VK_K   = 37
	VK_L   = 38
	VK_Z   = 44
	VK_X   = 45
	VK_C   = 46
	VK_V   = 47
	VK_B   = 48
	VK_N   = 49
	VK_M   = 50
	VK_F1  = 59
	VK_F2  = 60
	VK_F3  = 61
	VK_F4  = 62
	VK_F5  = 63
	VK_F6  = 64
	VK_F7  = 65
	VK_F8  = 66
	VK_F9  = 67
	VK_F10 = 68
	VK_F11 = 87
	VK_F12 = 88

	VK_F13 = 0x7C + 0xFFF
	VK_F14 = 0x7D + 0xFFF
	VK_F15 = 0x7E + 0xFFF
	VK_F16 = 0x7F + 0xFFF
	VK_F17 = 0x80 + 0xFFF
	VK_F18 = 0x81 + 0xFFF
	VK_F19 = 0x82 + 0xFFF
	VK_F20 = 0x83 + 0xFFF
	VK_F21 = 0x84 + 0xFFF
	VK_F22 = 0x85 + 0xFFF
	VK_F23 = 0x86 + 0xFFF
	VK_F24 = 0x87 + 0xFFF

	VK_NUMLOCK    = 69
	VK_SCROLLLOCK = 70
	VK_RESERVED   = 0
	VK_MINUS      = 12
	VK_EQUAL      = 13
	VK_BACKSPACE  = 14
	VK_TAB        = 15
	VK_LEFTBRACE  = 26
	VK_RIGHTBRACE = 27
	VK_ENTER      = 28
	VK_SEMICOLON  = 39
	VK_APOSTROPHE = 40
	VK_GRAVE      = 41
	VK_BACKSLASH  = 43
	VK_COMMA      = 51
	VK_DOT        = 52
	VK_SLASH      = 53
	VK_KPASTERISK = 55
	VK_SPACE      = 57
	VK_CAPSLOCK   = 58

	VK_KP0     = 82
	VK_KP1     = 79
	VK_KP2     = 80
	VK_KP3     = 81
	VK_KP4     = 75
	VK_KP5     = 76
	VK_KP6     = 77
	VK_KP7     = 71
	VK_KP8     = 72
	VK_KP9     = 73
	VK_KPMINUS = 74
	VK_KPPLUS  = 78
	VK_KPDOT   = 83

	// I add 0xFFF for all Virtual key
	VK_LBUTTON    = 0x01 + 0xFFF
	VK_RBUTTON    = 0x02 + 0xFFF
	VK_CANCEL     = 0x03 + 0xFFF
	VK_MBUTTON    = 0x04 + 0xFFF
	VK_XBUTTON1   = 0x05 + 0xFFF
	VK_XBUTTON2   = 0x06 + 0xFFF
	VK_BACK       = 0x08 + 0xFFF
	VK_CLEAR      = 0x0C + 0xFFF
	VK_PAUSE      = 0x13 + 0xFFF
	VK_CAPITAL    = 0x14 + 0xFFF
	VK_KANA       = 0x15 + 0xFFF
	VK_HANGUEL    = 0x15 + 0xFFF
	VK_HANGUL     = 0x15 + 0xFFF
	VK_JUNJA      = 0x17 + 0xFFF
	VK_FINAL      = 0x18 + 0xFFF
	VK_HANJA      = 0x19 + 0xFFF
	VK_KANJI      = 0x19 + 0xFFF
	VK_CONVERT    = 0x1C + 0xFFF
	VK_NONCONVERT = 0x1D + 0xFFF
	VK_ACCEPT     = 0x1E + 0xFFF
	VK_MODECHANGE = 0x1F + 0xFFF
	VK_PAGEUP     = 0x21 + 0xFFF
	VK_PAGEDOWN   = 0x22 + 0xFFF
	VK_END        = 0x23 + 0xFFF
	VK_HOME       = 0x24 + 0xFFF
	VK_LEFT       = 0x25 + 0xFFF
	VK_UP         = 0x26 + 0xFFF
	VK_RIGHT      = 0x27 + 0xFFF
	VK_DOWN       = 0x28 + 0xFFF
	VK_SELECT     = 0x29 + 0xFFF
	VK_PRINT      = 0x2A + 0xFFF
	VK_EXECUTE    = 0x2B + 0xFFF
	VK_SNAPSHOT   = 0x2C + 0xFFF
	VK_INSERT     = 0x2D + 0xFFF
	VK_DELETE     = 0x2E + 0xFFF
	VK_HELP       = 0x2F + 0xFFF

	VK_SCROLL              = 0x91 + 0xFFF
	VK_LMENU               = 0xA4 + 0xFFF
	VK_RMENU               = 0xA5 + 0xFFF
	VK_BROWSER_BACK        = 0xA6 + 0xFFF
	VK_BROWSER_FORWARD     = 0xA7 + 0xFFF
	VK_BROWSER_REFRESH     = 0xA8 + 0xFFF
	VK_BROWSER_STOP        = 0xA9 + 0xFFF
	VK_BROWSER_SEARCH      = 0xAA + 0xFFF
	VK_BROWSER_FAVORITES   = 0xAB + 0xFFF
	VK_BROWSER_HOME        = 0xAC + 0xFFF
	VK_VOLUME_MUTE         = 0xAD + 0xFFF
	VK_VOLUME_DOWN         = 0xAE + 0xFFF
	VK_VOLUME_UP           = 0xAF + 0xFFF
	VK_MEDIA_NEXT_TRACK    = 0xB0 + 0xFFF
	VK_MEDIA_PREV_TRACK    = 0xB1 + 0xFFF
	VK_MEDIA_STOP          = 0xB2 + 0xFFF
	VK_MEDIA_PLAY_PAUSE    = 0xB3 + 0xFFF
	VK_LAUNCH_MAIL         = 0xB4 + 0xFFF
	VK_LAUNCH_MEDIA_SELECT = 0xB5 + 0xFFF
	VK_LAUNCH_APP1         = 0xB6 + 0xFFF
	VK_LAUNCH_APP2         = 0xB7 + 0xFFF
	VK_OEM_1               = 0xBA + 0xFFF
	VK_OEM_PLUS            = 0xBB + 0xFFF
	VK_OEM_COMMA           = 0xBC + 0xFFF
	VK_OEM_MINUS           = 0xBD + 0xFFF
	VK_OEM_PERIOD          = 0xBE + 0xFFF
	VK_OEM_2               = 0xBF + 0xFFF
	VK_OEM_3               = 0xC0 + 0xFFF
	VK_OEM_4               = 0xDB + 0xFFF
	VK_OEM_5               = 0xDC + 0xFFF
	VK_OEM_6               = 0xDD + 0xFFF
	VK_OEM_7               = 0xDE + 0xFFF
	VK_OEM_8               = 0xDF + 0xFFF
	VK_OEM_102             = 0xE2 + 0xFFF
	VK_PROCESSKEY          = 0xE5 + 0xFFF
	VK_PACKET              = 0xE7 + 0xFFF
	VK_ATTN                = 0xF6 + 0xFFF
	VK_CRSEL               = 0xF7 + 0xFFF
	VK_EXSEL               = 0xF8 + 0xFFF
	VK_EREOF               = 0xF9 + 0xFFF
	VK_PLAY                = 0xFA + 0xFFF
	VK_ZOOM                = 0xFB + 0xFFF
	VK_NONAME              = 0xFC + 0xFFF
	VK_PA1                 = 0xFD + 0xFFF
	VK_OEM_CLEAR           = 0xFE + 0xFFF
)

Variables

This section is empty.

Functions

This section is empty.

Types

type KeyBonding

type KeyBonding struct {
	// contains filtered or unexported fields
}

KeyBonding type for keybd_event

func NewKeyBonding

func NewKeyBonding() (KeyBonding, error)

NewKeyBonding Use for create struct KeyBounding

Example
package main

import (
	"runtime"
	"time"

	"github.com/micmonay/keybd_event"
)

func main() {
	kb, err := keybd_event.NewKeyBonding()
	if err != nil {
		panic(err)
	}

	// For linux, it is very important to wait 2 seconds
	if runtime.GOOS == "linux" {
		time.Sleep(2 * time.Second)
	}

	// Select keys to be pressed
	kb.SetKeys(keybd_event.VK_A, keybd_event.VK_B)

	// Set shift to be pressed
	kb.HasSHIFT(true)

	// Press the selected keys
	err = kb.Launching()
	if err != nil {
		panic(err)
	}

	// Or you can use Press and Release
	kb.Press()
	time.Sleep(10 * time.Millisecond)
	kb.Release()

	// Here, the program will generate "ABAB" as if they were pressed on the keyboard.

}

func (*KeyBonding) AddKey

func (k *KeyBonding) AddKey(key int)

AddKey add one key pressed

func (*KeyBonding) Clear

func (k *KeyBonding) Clear()

Clear clean instance

func (*KeyBonding) HasALT

func (k *KeyBonding) HasALT(b bool)

HasALT If key ALT pressed

func (*KeyBonding) HasALTGR

func (k *KeyBonding) HasALTGR(b bool)

HasALTGR If key ALTGR pressed

func (*KeyBonding) HasCTRL

func (k *KeyBonding) HasCTRL(b bool)

HasCTRL If key CTRL pressed

func (*KeyBonding) HasCTRLR

func (k *KeyBonding) HasCTRLR(b bool)

HasCTRLR If key CTRLR pressed

This is currently not supported on macOS

func (*KeyBonding) HasSHIFT

func (k *KeyBonding) HasSHIFT(b bool)

HasSHIFT If key SHIFT pressed

func (*KeyBonding) HasSHIFTR

func (k *KeyBonding) HasSHIFTR(b bool)

HasSHIFTR If key SHIFTR pressed

This is currently not supported on macOS

func (*KeyBonding) HasSuper

func (k *KeyBonding) HasSuper(b bool)

HasSuper If key Super pressed

func (*KeyBonding) Launching

func (k *KeyBonding) Launching() error

Launch key bounding

func (*KeyBonding) Press added in v1.1.0

func (k *KeyBonding) Press() error

Press key(s)

func (*KeyBonding) Release added in v1.1.0

func (k *KeyBonding) Release() error

Release key(s)

func (*KeyBonding) SetKeys

func (k *KeyBonding) SetKeys(keys ...int)

SetKeys set keys

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL