Speeding up virtual keycode scanning

Here is the problem:

GetAsyncKeyState = Win32API.new(‘user32’,‘GetAsyncKeyState’, [‘i’], ‘i’)

while true

keys.each {|k, v| print k.upcase if GetAsyncKeyState.call(v) & 0x01 ==
1 && GetAsyncKeyState.call(0x10) & 0x01 == 1}
keys.each {|k, v| print k.downcase if GetAsyncKeyState.call(v) & 0x01
== 1}

end

##keys represents a hash of scan code to virtual keycode conversions.

The problem occurs by running this in a console… pressing “a”
too quickly results in no secondary “a”.

In short… it doesn’t pick up all the keyboard input unless you type at
1 letter per 3 seconds. Almost like it is cycling through the virtual
keycodes and doesn’t get back around to “a” in time.

Any way to speed this up?

Regards,

  • Mac

On Jul 6, 2009, at 22:22 , Michael L. wrote:

0x01 == 1 && GetAsyncKeyState.call(0x10) & 0x01 == 1}
In short… it doesn’t pick up all the keyboard input unless you
type at
1 letter per 3 seconds. Almost like it is cycling through the virtual
keycodes and doesn’t get back around to “a” in time.

Not “almost like”: Exactly like! You have this TWICE per loop:

keys.each { … }

so you’re cycling through all the virtual keycodes twice per loop and
you’re calling GetAsyncKeyState at least keys.size * 2 times per loop.
I’m sure you can rewrite this to only have one call to each per loop
with minimal effort. I doubt that you need to have both upcase and
downcase. Prolly only one is needed depending on the keys in keys, but
that won’t speed up much at all.

My guess is that there is a much more efficient way to do what you
want to do… like, don’t put everything in keys, just the 5-10 you’re
actually interested in (assuming you’re doing a game or something)…

I’ll also bet that whatever you do doesn’t work with the dvorak
layout, which would make me sad, except that you’re developing on a
platform I try not to use to begin with. :stuck_out_tongue:

P.S. Use a constant instead of 0x10
P.P.S. It’d be nice if you got your return/enter key fixed.
P.P.P.S. loop do … end

Hi,

I tried to build Ruby 1.9.1p129 on a 64 bit openSUSE 11.0 system.
It failed with the following message.
Is this a bug in Ruby 1.9.1p129?

./miniruby -I./lib -I.ext/common -I./- -r./ext/purelib.rb
./enc/make_encmake.rb --builtin-encs=“ascii.o us_ascii.o unicode.o
utf_8.o” --builtin-transes=“newline.o” enc.mk
/home/rc/Ruby/download/ruby-1.9.1-p129/rbconfig.rb:177: [BUG] Stack
consistency error (sp: 110, bp: 108)
ruby 1.9.1p129 (2009-05-12 revision 23412) [x86_64-linux]

– control frame ----------
c:0025 p:0178 s:0110 b:0108 l:000094 d:000107 BLOCK
/home/rc/Ruby/download/ruby-1.9.1-p129/rbconfig.rb:177
c:0024 p:---- s:0101 b:0101 l:000100 d:000100 FINISH
c:0023 p:---- s:0099 b:0099 l:000098 d:000098 CFUNC :gsub!
c:0022 p:0023 s:0095 b:0095 l:000094 d:000094 METHOD
/home/rc/Ruby/download/ruby-1.9.1-p129/rbconfig.rb:173
c:0021 p:0110 s:0090 b:0088 l:000074 d:000087 BLOCK
/home/rc/Ruby/download/ruby-1.9.1-p129/rbconfig.rb:180
c:0020 p:---- s:0081 b:0081 l:000080 d:000080 FINISH
c:0019 p:---- s:0079 b:0079 l:000078 d:000078 CFUNC :gsub!
c:0018 p:0023 s:0075 b:0075 l:000074 d:000074 METHOD
/home/rc/Ruby/download/ruby-1.9.1-p129/rbconfig.rb:173
c:0017 p:0110 s:0070 b:0068 l:000054 d:000067 BLOCK
/home/rc/Ruby/download/ruby-1.9.1-p129/rbconfig.rb:180
c:0016 p:---- s:0061 b:0061 l:000060 d:000060 FINISH
c:0015 p:---- s:0059 b:0059 l:000058 d:000058 CFUNC :gsub!
c:0014 p:0023 s:0055 b:0055 l:000054 d:000054 METHOD
/home/rc/Ruby/download/ruby-1.9.1-p129/rbconfig.rb:173
c:0013 p:0018 s:0050 b:0050 l:000041 d:000049 BLOCK
/home/rc/Ruby/download/ruby-1.9.1-p129/rbconfig.rb:191
c:0012 p:---- s:0047 b:0047 l:000046 d:000046 FINISH
c:0011 p:---- s:0045 b:0045 l:000044 d:000044 CFUNC :each_value
c:0010 p:3351 s:0042 b:0042 l:000041 d:000041 CLASS
/home/rc/Ruby/download/ruby-1.9.1-p129/rbconfig.rb:190
c:0009 p:0009 s:0040 b:0040 l:000039 d:000039 TOP
/home/rc/Ruby/download/ruby-1.9.1-p129/rbconfig.rb:5
c:0008 p:---- s:0038 b:0038 l:000037 d:000037 FINISH
c:0007 p:---- s:0036 b:0036 l:000035 d:000035 CFUNC :require
c:0006 p:0011 s:0032 b:0032 l:000031 d:000031 TOP
/home/rc/Ruby/download/ruby-1.9.1-p129/lib/mkmf.rb:4
c:0005 p:---- s:0018 b:0018 l:000017 d:000017 FINISH
c:0004 p:---- s:0016 b:0016 l:000015 d:000015 CFUNC :load
c:0003 p:0104 s:0012 b:0012 l:0019b8 d:000638 EVAL
./enc/make_encmake.rb:8
c:0002 p:---- s:0004 b:0004 l:000003 d:000003 FINISH
c:0001 p:0000 s:0002 b:0002 l:0019b8 d:0019b8 TOP

– Ruby level backtrace
information-----------------------------------------
/home/rc/Ruby/download/ruby-1.9.1-p129/rbconfig.rb:177:in block in expand' /home/rc/Ruby/download/ruby-1.9.1-p129/rbconfig.rb:173:in gsub!’
/home/rc/Ruby/download/ruby-1.9.1-p129/rbconfig.rb:173:in expand' /home/rc/Ruby/download/ruby-1.9.1-p129/rbconfig.rb:180:in block in
expand’
/home/rc/Ruby/download/ruby-1.9.1-p129/rbconfig.rb:173:in gsub!' /home/rc/Ruby/download/ruby-1.9.1-p129/rbconfig.rb:173:in expand’
/home/rc/Ruby/download/ruby-1.9.1-p129/rbconfig.rb:180:in block in expand' /home/rc/Ruby/download/ruby-1.9.1-p129/rbconfig.rb:173:in gsub!’
/home/rc/Ruby/download/ruby-1.9.1-p129/rbconfig.rb:173:in expand' /home/rc/Ruby/download/ruby-1.9.1-p129/rbconfig.rb:191:in block in
module:RbConfig
/home/rc/Ruby/download/ruby-1.9.1-p129/rbconfig.rb:190:in each_value' /home/rc/Ruby/download/ruby-1.9.1-p129/rbconfig.rb:190:in module:RbConfig
/home/rc/Ruby/download/ruby-1.9.1-p129/rbconfig.rb:5:in <top (required)>' /home/rc/Ruby/download/ruby-1.9.1-p129/lib/mkmf.rb:4:in require’
/home/rc/Ruby/download/ruby-1.9.1-p129/lib/mkmf.rb:4:in <top (required)>' ./enc/make_encmake.rb:8:in load’
./enc/make_encmake.rb:8:in `’

– C level backtrace information

0x51afcb ./miniruby(rb_vm_bugreport+0x3b) [0x51afcb]
0x43f86e ./miniruby [0x43f86e]
0x43f9d1 ./miniruby(rb_bug+0xb1) [0x43f9d1]
0x5103ee ./miniruby [0x5103ee]
0x510b04 ./miniruby [0x510b04]
0x517918 ./miniruby(rb_yield+0x378) [0x517918]
0x4dbd1f ./miniruby [0x4dbd1f]
0x515d42 ./miniruby [0x515d42]
0x50b894 ./miniruby [0x50b894]
0x510b04 ./miniruby [0x510b04]
0x517918 ./miniruby(rb_yield+0x378) [0x517918]
0x4dbd1f ./miniruby [0x4dbd1f]
0x515d42 ./miniruby [0x515d42]
0x50b894 ./miniruby [0x50b894]
0x510b04 ./miniruby [0x510b04]
0x517918 ./miniruby(rb_yield+0x378) [0x517918]
0x4dbd1f ./miniruby [0x4dbd1f]
0x515d42 ./miniruby [0x515d42]
0x50b894 ./miniruby [0x50b894]
0x510b04 ./miniruby [0x510b04]
0x517918 ./miniruby(rb_yield+0x378) [0x517918]
0x453002 ./miniruby [0x453002]
0x453843 ./miniruby [0x453843]
0x4ca634 ./miniruby(st_foreach+0xd4) [0x4ca634]
0x4555c8 ./miniruby [0x4555c8]
0x440cfb ./miniruby(rb_ensure+0xab) [0x440cfb]
0x454a62 ./miniruby(rb_hash_foreach+0x32) [0x454a62]
0x454f1c ./miniruby [0x454f1c]
0x515d42 ./miniruby [0x515d42]
0x50b894 ./miniruby [0x50b894]
0x510b04 ./miniruby [0x510b04]
0x510da0 ./miniruby(rb_iseq_eval+0x120) [0x510da0]
0x44381f ./miniruby(rb_load+0x17f) [0x44381f]
0x44458f ./miniruby(rb_require_safe+0x5ef) [0x44458f]
0x515d42 ./miniruby [0x515d42]
0x50b894 ./miniruby [0x50b894]
0x510b04 ./miniruby [0x510b04]
0x510da0 ./miniruby(rb_iseq_eval+0x120) [0x510da0]
0x44381f ./miniruby(rb_load+0x17f) [0x44381f]
0x443943 ./miniruby [0x443943]
0x515d42 ./miniruby [0x515d42]
0x50b894 ./miniruby [0x50b894]
0x510b04 ./miniruby [0x510b04]
0x510c49 ./miniruby(rb_iseq_eval_main+0xa9) [0x510c49]
0x44199c ./miniruby(ruby_exec_node+0xac) [0x44199c]
0x442f83 ./miniruby(ruby_run_node+0x33) [0x442f83]
0x4183ed ./miniruby(main+0x4d) [0x4183ed]
0x2af344fc1436 /lib64/libc.so.6(__libc_start_main+0xe6) [0x2af344fc1436]
0x4182d9 ./miniruby [0x4182d9]

[NOTE]
You may encounter a bug of Ruby interpreter. Bug reports are welcome.
For details: http://www.ruby-lang.org/bugreport.html

make: *** [enc.mk] Aborted

On Tue, Jul 7, 2009 at 1:14 PM, Ryan D. [email protected]
wrote:

./enc/make_encmake.rb --builtin-encs="ascii.o us_ascii.o unicode.o
Say Ryan,
I’m confused where the thread-hijack is in this? I recieved the message
as
an original. shrug

Good luck Roland.

Peter F.
(847) 687-7646
Email: [email protected]
IM GTalk: peter.fitzgibbons
IM AOL: [email protected]

On Jul 7, 2009, at 06:30 , Roland Cruesemann wrote:

consistency error (sp: 110, bp: 108)
ruby 1.9.1p129 (2009-05-12 revision 23412) [x86_64-linux]

  1. Don’t thread-hijack, ever.
  2. Any crash of pure ruby code or during the build is a bug and should
    go to ruby-core@ instead of here.

On Jul 23, 2009, at 19:05 , Peter F. wrote:

Say Ryan,
I’m confused where the thread-hijack is in this? I recieved the
message as
an original. shrug

Good luck Roland.

Message-Id: [email protected]

In-Reply-To: [email protected]

References: [email protected]