mac-key_code 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d82745a5c02d07bc21ecabdab71f9a27fa846446
4
+ data.tar.gz: 3969b36bf047625a4fdaa36add24f99d15d63863
5
+ SHA512:
6
+ metadata.gz: 6cd0fe6108e686e102aee0e06d6875f551c455f9d57a284b360e099440980cca1da07c8d12b9746bfdd5cc40311599dbda37ec542980c48c1e58149c3232b4d9
7
+ data.tar.gz: cd45bfabb68387bc1b33ab627f687393f18e40783436f4fd3a52e8a1818f9d61ad72f29ea109976dd10de72416a340da7c805101e3b7a2f8d8caf6bb66f71dea
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.log
20
+ *.o
21
+ Makefile
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mac-key_code.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 youpy
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # mac-key_code
2
+
3
+ A key code library for mac.
4
+ It supports any keyboard layouts such as QWERTY, Dvorak etc.
5
+
6
+ It is based on http://stackoverflow.com/a/1971027
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'mac-key_code'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install mac-key_code
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ require 'mac-key_code'
26
+
27
+ key_code = Mac::KeyCode.new(38)
28
+ key_code.to_s # => 'j'
29
+ ```
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it ( http://github.com/youpy/mac-key_code/fork )
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+ require 'rake'
2
+ require 'rake/clean'
3
+ require "bundler/gem_tasks"
4
+
5
+ ext_names = %w/key_code_native/
6
+ ext_names.each do |ext_name|
7
+ CLEAN.include Dir.glob("ext/#{ext_name}/*{.o,.log}")
8
+ CLEAN.include "ext/#{ext_name}/Makefile"
9
+ CLOBBER.include "ext/#{ext_name}/#{ext_name}.bundle"
10
+ CLOBBER.include "lib/#{ext_name}.bundle"
11
+
12
+ file "lib/#{ext_name}.bundle" =>
13
+ Dir.glob("ext/#{ext_name}/*{.rb,.c}") do
14
+ Dir.chdir("ext/#{ext_name}") do
15
+ ruby "extconf.rb"
16
+ sh "make"
17
+ end
18
+ cp "ext/#{ext_name}/#{ext_name}.bundle", "lib/"
19
+ end
20
+
21
+ task :spec => "lib/#{ext_name}.bundle"
22
+ end
23
+
24
+ require 'rspec/core/rake_task'
25
+ RSpec::Core::RakeTask.new(:spec) do |spec|
26
+ spec.pattern = FileList['spec/**/*_spec.rb']
27
+ end
28
+
29
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ require "mkmf"
2
+
3
+ extension_name = 'key_code_native'
4
+ dir_config(extension_name)
5
+
6
+ $LDFLAGS += ' -framework AppKit'
7
+
8
+ begin
9
+ MACRUBY_VERSION # Only MacRuby has this constant.
10
+ $CFLAGS += ' -fobjc-gc' # Enable MacOSX's GC for MacRuby
11
+ rescue
12
+ end
13
+
14
+ create_makefile(extension_name)
@@ -0,0 +1,115 @@
1
+ #include <CoreFoundation/CoreFoundation.h>
2
+ #include <Carbon/Carbon.h> /* For kVK_ constants, and TIS functions. */
3
+ #include <ruby.h>
4
+
5
+ /* based on http://stackoverflow.com/a/1971027 */
6
+
7
+ VALUE CFString2RString(CFStringRef str) {
8
+ int stringSize;
9
+ char *tmpptr;
10
+ VALUE result;
11
+
12
+ stringSize = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str), kCFStringEncodingUTF8) + 1;
13
+ tmpptr = (char *)malloc(sizeof(char) * stringSize);
14
+ CFStringGetCString(str, tmpptr, stringSize, kCFStringEncodingUTF8);
15
+ result = rb_str_new2(tmpptr);
16
+ free(tmpptr);
17
+
18
+ return result;
19
+ }
20
+
21
+ /* Returns string representation of key, if it is printable.
22
+ * Ownership follows the Create Rule; that is, it is the caller's
23
+ * responsibility to release the returned object. */
24
+ CFStringRef createStringForKey(CGKeyCode keyCode)
25
+ {
26
+ TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardInputSource();
27
+ CFDataRef layoutData =
28
+ TISGetInputSourceProperty(currentKeyboard,
29
+ kTISPropertyUnicodeKeyLayoutData);
30
+ const UCKeyboardLayout *keyboardLayout =
31
+ (const UCKeyboardLayout *)CFDataGetBytePtr(layoutData);
32
+
33
+ UInt32 keysDown = 0;
34
+ UniChar chars[4];
35
+ UniCharCount realLength;
36
+
37
+ UCKeyTranslate(keyboardLayout,
38
+ keyCode,
39
+ kUCKeyActionDisplay,
40
+ 0,
41
+ LMGetKbdType(),
42
+ kUCKeyTranslateNoDeadKeysBit,
43
+ &keysDown,
44
+ sizeof(chars) / sizeof(chars[0]),
45
+ &realLength,
46
+ chars);
47
+ CFRelease(currentKeyboard);
48
+
49
+ return CFStringCreateWithCharacters(kCFAllocatorDefault, chars, 1);
50
+ }
51
+
52
+ /* Returns key code for given character via the above function, or UINT16_MAX
53
+ * on error. */
54
+ CGKeyCode keyCodeForChar(const char c)
55
+ {
56
+ static CFMutableDictionaryRef charToCodeDict = NULL;
57
+ CGKeyCode code;
58
+ UniChar character = c;
59
+ CFStringRef charStr = NULL;
60
+
61
+ /* Generate table of keycodes and characters. */
62
+ if (charToCodeDict == NULL) {
63
+ size_t i;
64
+ charToCodeDict = CFDictionaryCreateMutable(kCFAllocatorDefault,
65
+ 128,
66
+ &kCFCopyStringDictionaryKeyCallBacks,
67
+ NULL);
68
+ if (charToCodeDict == NULL) return UINT16_MAX;
69
+
70
+ /* Loop through every keycode (0 - 127) to find its current mapping. */
71
+ for (i = 0; i < 128; ++i) {
72
+ CFStringRef string = createStringForKey((CGKeyCode)i);
73
+ if (string != NULL) {
74
+ CFDictionaryAddValue(charToCodeDict, string, (const void *)i);
75
+ CFRelease(string);
76
+ }
77
+ }
78
+ }
79
+
80
+ charStr = CFStringCreateWithCharacters(kCFAllocatorDefault, &character, 1);
81
+
82
+ /* Our values may be NULL (0), so we need to use this function. */
83
+ if (!CFDictionaryGetValueIfPresent(charToCodeDict, charStr,
84
+ (const void **)&code)) {
85
+ code = UINT16_MAX;
86
+ }
87
+
88
+ CFRelease(charStr);
89
+ return code;
90
+ }
91
+
92
+ static VALUE mKeyCodeNative_key_code_to_char(int argc, VALUE *argv, VALUE self)
93
+ {
94
+ VALUE key_code;
95
+
96
+ int intKey_code;
97
+ CFStringRef c;
98
+
99
+ rb_scan_args(argc, argv, "1", &key_code);
100
+
101
+
102
+ intKey_code = NUM2INT(key_code);
103
+
104
+ c = createStringForKey((CGKeyCode)intKey_code);
105
+
106
+ return CFString2RString(c);
107
+ }
108
+
109
+ void Init_key_code_native(void){
110
+ VALUE rb_mMac, rb_mKeyCodeNative;
111
+
112
+ rb_mMac = rb_define_module("Mac");
113
+ rb_mKeyCodeNative = rb_define_module_under(rb_mMac, "KeyCodeNative");
114
+ rb_define_singleton_method(rb_mKeyCodeNative, "key_code_to_char", mKeyCodeNative_key_code_to_char, -1);
115
+ }
@@ -0,0 +1,5 @@
1
+ module Mac
2
+ class KeyCode
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,19 @@
1
+ require "mac/key_code/version"
2
+ require "key_code_native"
3
+
4
+ module Mac
5
+ class KeyCode
6
+ def initialize(key_code)
7
+ @key_code = key_code
8
+ @char = KeyCodeNative.key_code_to_char(@key_code)
9
+ end
10
+
11
+ def printable?
12
+ @char =~ /^[[:print:]\s]+/
13
+ end
14
+
15
+ def to_s
16
+ @char
17
+ end
18
+ end
19
+ end
@@ -0,0 +1 @@
1
+ require 'mac/key_code'
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mac/key_code/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mac-key_code"
8
+ spec.version = Mac::KeyCode::VERSION
9
+ spec.authors = ["youpy"]
10
+ spec.email = ["youpy@buycheapviagraonlinenow.com"]
11
+ spec.summary = %q{A key code library for mac}
12
+ spec.description = %q{A key code library for mac. It supports any keyboard layouts such as QWERTY, Dvorak etc.}
13
+ spec.homepage = "https://github.com/youpy/mac-key_code"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec", "~> 2.14"
24
+ end
@@ -0,0 +1,79 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ include Mac
4
+
5
+ # TODO: support key lauouts other than QWERTY
6
+
7
+ describe KeyCode do
8
+ describe '.new' do
9
+ subject do
10
+ KeyCode.new(key_code)
11
+ end
12
+
13
+ context 'key code for space key' do
14
+ let(:key_code) do
15
+ 49
16
+ end
17
+
18
+ it do
19
+ should be_printable
20
+ end
21
+
22
+ it do
23
+ subject.to_s.should eql(' ')
24
+ end
25
+ end
26
+
27
+ context 'key code for tab key' do
28
+ let(:key_code) do
29
+ 48
30
+ end
31
+
32
+ it do
33
+ should be_printable
34
+ end
35
+
36
+ it do
37
+ subject.to_s.should eql("\t")
38
+ end
39
+ end
40
+
41
+ context 'key code for "j"' do
42
+ let(:key_code) do
43
+ 38
44
+ end
45
+
46
+ it do
47
+ should be_printable
48
+ end
49
+
50
+ it do
51
+ subject.to_s.should eql('j')
52
+ end
53
+ end
54
+
55
+ context 'key code for return key' do
56
+ let(:key_code) do
57
+ 36
58
+ end
59
+
60
+ it do
61
+ should be_printable
62
+ end
63
+
64
+ it do
65
+ subject.to_s.should eql("\r")
66
+ end
67
+ end
68
+
69
+ context 'key code for non-printable key' do
70
+ let(:key_code) do
71
+ 96
72
+ end
73
+
74
+ it do
75
+ should_not be_printable
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,21 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+
3
+ require 'mac-key_code'
4
+
5
+ # This file was generated by the `rspec --init` command. Conventionally, all
6
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
7
+ # Require this file using `require "spec_helper"` to ensure that it is only
8
+ # loaded once.
9
+ #
10
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
11
+ RSpec.configure do |config|
12
+ config.treat_symbols_as_metadata_keys_with_true_values = true
13
+ config.run_all_when_everything_filtered = true
14
+ config.filter_run :focus
15
+
16
+ # Run specs in random order to surface order dependencies. If you find an
17
+ # order dependency and want to debug it, you can fix the order by providing
18
+ # the seed, which is printed after each run.
19
+ # --seed 1234
20
+ config.order = 'random'
21
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mac-key_code
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - youpy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.14'
55
+ description: A key code library for mac. It supports any keyboard layouts such as
56
+ QWERTY, Dvorak etc.
57
+ email:
58
+ - youpy@buycheapviagraonlinenow.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - ext/key_code_native/extconf.rb
70
+ - ext/key_code_native/key_code_native.c
71
+ - lib/mac-key_code.rb
72
+ - lib/mac/key_code.rb
73
+ - lib/mac/key_code/version.rb
74
+ - mac-key_code.gemspec
75
+ - spec/mac/key_code_spec.rb
76
+ - spec/spec_helper.rb
77
+ homepage: https://github.com/youpy/mac-key_code
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.2.1
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: A key code library for mac
101
+ test_files:
102
+ - spec/mac/key_code_spec.rb
103
+ - spec/spec_helper.rb