rukeyboard 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d55be4ce8e35226b329786a62a7ec7c20ab92484
4
+ data.tar.gz: e7b60cd680125e3c28172f423e3b56dff4d8979e
5
+ SHA512:
6
+ metadata.gz: 64a3313cf78e5c3cf022969f5af2565a2134a3777e2a1300ec0d81f3713cfb155a87738992d612c972a92272d73713d3693ab66c7ff40f3aa03caaa372841e0d
7
+ data.tar.gz: c18b361bac30376866ec2ce5ace1c7a45ee913825bb9414d292c5b911385e4ca1d5bd42f4825f4fe9add6e3e9ec4f21c5fb0aec14dfa1a97d2183d0de5bb005e
@@ -0,0 +1,36 @@
1
+ require 'rbconfig'
2
+
3
+ class RuKeyboard
4
+ def press_key key
5
+ raise NotImplementedError
6
+ end
7
+
8
+ def release_key key
9
+ raise NotImplementedError
10
+ end
11
+
12
+ def tap_key key, n = 1
13
+ n.times do
14
+ press_key key
15
+ release_key key
16
+ end
17
+ end
18
+
19
+ def type_string str, interval=0
20
+ str.chars.each do |ch|
21
+ sleep interval
22
+ tap_key ch
23
+ end
24
+ end
25
+ end
26
+
27
+ os = RbConfig::CONFIG['host_os']
28
+
29
+ case os
30
+ when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
31
+ require 'rukeyboard/win32.rb'
32
+ when /darwin|mac os/
33
+ require 'rukeyboard/darwin.rb'
34
+ when /linux|bsd/
35
+ require 'rukeyboard/x11.rb'
36
+ end
@@ -0,0 +1,56 @@
1
+ require 'ffi'
2
+
3
+ module CGEvent
4
+ extend FFI::Library
5
+ ffi_lib "/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics"
6
+
7
+ enum :CGEventTapLocation, [:kCGHIDEventTap, 0,
8
+ :kCGSessionEventTap,
9
+ :kCGAnnotatedSessionEventTap ]
10
+
11
+ CHARS = {
12
+ 'a' => 0x00, 's' => 0x01, 'd' => 0x02, 'f' => 0x03, 'h' => 0x04,
13
+ 'g' => 0x05, 'z' => 0x06, 'x' => 0x07, 'c' => 0x08, 'v' => 0x09,
14
+ 'b' => 0x0b, 'q' => 0x0c, 'w' => 0x0d, 'e' => 0x0e, 'r' => 0x0f,
15
+ 'y' => 0x10, 't' => 0x11, '1' => 0x12, '2' => 0x13, '3' => 0x14,
16
+ '4' => 0x15, '6' => 0x16, '5' => 0x17, '=' => 0x18, '9' => 0x19,
17
+ '7' => 0x1a, '-' => 0x1b, '8' => 0x1c, '0' => 0x1d, ']' => 0x1e,
18
+ 'o' => 0x1f, 'u' => 0x20, '[' => 0x21, 'i' => 0x22, 'p' => 0x23,
19
+ 'l' => 0x25, 'j' => 0x26, '\'' => 0x27, 'k' => 0x28, ';' => 0x29,
20
+ '\\' => 0x2a, ',' => 0x2b, '/' => 0x2c, 'n' => 0x2d, 'm' => 0x2e,
21
+ '.' => 0x2f, '`' => 0x32, ' ' => 0x31, '\r' => 0x24, '\t' => 0x30,
22
+ 'shift' => 0x38
23
+ }
24
+
25
+ attach_function :CGEventCreateKeyboardEvent,
26
+ [:pointer, :char, :bool],
27
+ :pointer
28
+
29
+ attach_function :CGEventPost,
30
+ [:CGEventTapLocation, :pointer],
31
+ :void
32
+
33
+ attach_function :CFRelease,
34
+ [:pointer],
35
+ :void
36
+ end
37
+
38
+ class RuKeyboard
39
+ def press_key key
40
+ event key, true
41
+ end
42
+
43
+ def release_key key
44
+ event key, false
45
+ end
46
+
47
+ private
48
+
49
+ def event key, press
50
+ code = CGEvent::CHARS[key.downcase]
51
+ return if code.nil?
52
+ event = CGEvent::CGEventCreateKeyboardEvent nil, code, press
53
+ CGEvent::CGEventPost :kCGHIDEventTap, event
54
+ CGEvent::CFRelease event
55
+ end
56
+ end
@@ -0,0 +1,24 @@
1
+ require 'ffi'
2
+
3
+ module User32
4
+ extend FFI::Library
5
+ ffi_lib 'user32.dll'
6
+ ffi_convention :stdcall
7
+
8
+ KEYEVENTF_KEYUP = 0x0002
9
+
10
+ attach_function :keybd_event, [:short, :short, :uint, :pointer], :void
11
+ attach_function :VkKeyScanA, [:char], :short
12
+ end
13
+
14
+ class RuKeyboard
15
+ def press_key key
16
+ code = User32::VkKeyScanA key[0].ord
17
+ User32::keybd_event code, 0, 0, nil
18
+ end
19
+
20
+ def release_key key
21
+ code = User32::VkKeyScanA key[0].ord
22
+ User32::keybd_event code, 0, User32::KEYEVENTF_KEYUP, nil
23
+ end
24
+ end
File without changes
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rukeyboard
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Maxim Kostuchenko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ffi
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.9.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.9.3
27
+ description: Cross-platform solution for simulating keyboard events
28
+ email: kstmaks@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/rukeyboard.rb
34
+ - lib/rukeyboard/darwin.rb
35
+ - lib/rukeyboard/win32.rb
36
+ - lib/rukeyboard/x11.rb
37
+ homepage: ''
38
+ licenses:
39
+ - MIT
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.0.3
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Simulate key events from ruby.
61
+ test_files: []