keybd_event

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2019 License: MIT Imports: 5 Imported by: 1

README

keybd_event

This library simulates the key press on the keyboard.

Important :

  • The keys change in the different keyboard layout configuration of the target computer.
  • I have tested this code on the different system and I don't find the error, but I don't granted this update have no bug. If you have a bug, please create an issue.

For simulate key press in Linux, Windows and Mac in golang

An 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 wait 2 seconds
	if runtime.GOOS == "linux" {
		time.Sleep(2 * time.Second)
	}
	
	//set keys
	kb.SetKeys(keybd_event.VK_A, keybd_event.VK_B) 

	//set shif is pressed
	kb.HasSHIFT(true) 

	//launch
	err = kb.Launching() 
	if err != nil {
		panic(err)
	}
	//Ouput : AB
}

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

The next picture is a good solution to understand

keyboard.png

Linux

On Linux this library use uinput, but generally, on the major distributions, the uinput is only for the root user.

The easy solution is executing on root user or change permission by chmod, but it is not good.

You can follow the next example, for more security.

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 keybd_event, to waiting 2 seconds before running first keyboard actions

Darwin (MAC OS)

This library depends on the frameworks Apple, I did not find a solution for cross-compilation.

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  = 0x0A
	VK_SP2  = 0x1B
	VK_SP3  = 0x18
	VK_SP4  = 0x21
	VK_SP5  = 0x1E
	VK_SP6  = 0x29
	VK_SP7  = 0x27
	VK_SP8  = 0x2A
	VK_SP9  = 0x2B
	VK_SP10 = 0x2F
	VK_SP11 = 0x2C
	VK_SP12 = 0x32

	VK_A              = 0x00
	VK_S              = 0x01
	VK_D              = 0x02
	VK_F              = 0x03
	VK_H              = 0x04
	VK_G              = 0x05
	VK_Z              = 0x06
	VK_X              = 0x07
	VK_C              = 0x08
	VK_V              = 0x09
	VK_B              = 0x0B
	VK_Q              = 0x0C
	VK_W              = 0x0D
	VK_E              = 0x0E
	VK_R              = 0x0F
	VK_Y              = 0x10
	VK_T              = 0x11
	VK_1              = 0x12
	VK_2              = 0x13
	VK_3              = 0x14
	VK_4              = 0x15
	VK_6              = 0x16
	VK_5              = 0x17
	VK_EQUAL          = 0x18
	VK_9              = 0x19
	VK_7              = 0x1A
	VK_MINUS          = 0x1B
	VK_8              = 0x1C
	VK_0              = 0x1D
	VK_RightBracket   = 0x1E
	VK_O              = 0x1F
	VK_U              = 0x20
	VK_LeftBracket    = 0x21
	VK_I              = 0x22
	VK_P              = 0x23
	VK_L              = 0x25
	VK_J              = 0x26
	VK_Quote          = 0x27
	VK_K              = 0x28
	VK_SEMICOLON      = 0x29
	VK_BACKSLASH      = 0x2A
	VK_COMMA          = 0x2B
	VK_SLASH          = 0x2C
	VK_N              = 0x2D
	VK_M              = 0x2E
	VK_Period         = 0x2F
	VK_GRAVE          = 0x32
	VK_KeypadDecimal  = 0x41
	VK_KeypadMultiply = 0x43
	VK_KeypadPlus     = 0x45
	VK_KeypadClear    = 0x47
	VK_KeypadDivide   = 0x4B
	VK_KeypadEnter    = 0x4C
	VK_KeypadMinus    = 0x4E
	VK_KeypadEquals   = 0x51
	VK_Keypad0        = 0x52
	VK_Keypad1        = 0x53
	VK_Keypad2        = 0x54
	VK_Keypad3        = 0x55
	VK_Keypad4        = 0x56
	VK_Keypad5        = 0x57
	VK_Keypad6        = 0x58
	VK_Keypad7        = 0x59
	VK_Keypad8        = 0x5B
	VK_Keypad9        = 0x5C

	VK_ENTER         = 0x24
	VK_TAB           = 0x30
	VK_SPACE         = 0x31
	VK_DELETE        = 0x33
	VK_ESC           = 0x35
	VK_CAPSLOCK      = 0x39
	VK_Option        = 0x3A
	VK_RightOption   = 0x3D
	VK_Function      = 0x3F
	VK_F17           = 0x40
	VK_VOLUMEUP      = 0x48
	VK_VOLUMEDOWN    = 0x49
	VK_MUTE          = 0x4A
	VK_F18           = 0x4F
	VK_F19           = 0x50
	VK_F20           = 0x5A
	VK_F5            = 0x60
	VK_F6            = 0x61
	VK_F7            = 0x62
	VK_F3            = 0x63
	VK_F8            = 0x64
	VK_F9            = 0x65
	VK_F11           = 0x67
	VK_F13           = 0x69
	VK_F16           = 0x6A
	VK_F14           = 0x6B
	VK_F10           = 0x6D
	VK_F12           = 0x6F
	VK_F15           = 0x71
	VK_HELP          = 0x72
	VK_HOME          = 0x73
	VK_PAGEUP        = 0x74
	VK_ForwardDelete = 0x75
	VK_F4            = 0x76
	VK_END           = 0x77
	VK_F2            = 0x78
	VK_PAGEDOWN      = 0x79
	VK_F1            = 0x7A
	VK_LEFT          = 0x7B
	VK_RIGHT         = 0x7C
	VK_DOWN          = 0x7D
	VK_UP            = 0x7E
)

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
kb, err := NewKeyBonding()
if err != nil {
	panic(err)
}
// For linux, it is very important wait 2 seconds
if runtime.GOOS == "linux" {
	time.Sleep(2 * time.Second)
}
kb.SetKeys(VK_SP2) //set keys

kb.HasALTGR(true) //set shif is pressed

err = kb.Launching() //launch
if err != nil {
	panic(err)
}
Output:
AB

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) 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