class << Input
GetAsyncKeyState = Win32API.new('user32', 'GetAsyncKeyState', 'i','i')
Key_States = Hash.new {0}
KtK = {
LEFT: [0x41],
UP: [0x57],
RIGHT: [0x44],
DOWN: [0x53],
A: [0x4A],
B: [0x4B],
C: [0x4C, 0x0D, 0x5A],
X: [0x4D],
Y: [0x4E],
Z: [0x4F],
R: [0x50],
L: [0x51],
F9: [0x78],
CTRL: [0x11]
}
attr_reader :dir4, :dir8
def press?(sym)
KtK[sym].each {|s| return true if Key_States[s] > 0}
return false
end
def trigger?(sym)
KtK[sym].each {|s| return true if Key_States[s] == 1}
return false
end
def repeat?(sym)
KtK[sym].each {|s|
s = Key_States[s]
return true if s == 1 || s > 23 && s % 6 == 0
}
return false
end
#
def update
0xBE.times {|i|
GetAsyncKeyState.call(i) == 0 ? Key_States[i] = 0 : Key_States[i] += 1
}
update_dir_state
end
def update_dir_state
if press?(:DOWN)
if press?(:LEFT); @dir8 = 1
elsif press?(:RIGHT); @dir8 = 3
else @dir8 = 2
end
@dir4 = 2
elsif press?(:LEFT)
@dir8 = press?(:UP) ? 7 : 4
@dir4 = 4
elsif press?(:RIGHT)
@dir8 = press?(:UP) ? 9 : 6
@dir4 = 6
elsif press?(:UP)
@dir8 = 8
@dir4 = 8
else
@dir8 = 0
@dir4 = 0
end
end
end
class << Input
GetAsyncKeyState = Win32API.new('user32', 'GetAsyncKeyState', 'i','i')
Key_States = Hash.new {0}
KtK = {
LEFT: [0x41],
UP: [0x57],
RIGHT: [0x44],
DOWN: [0x53],
A: [0x4A],
B: [0x4B],
C: [0x4C, 0x0D, 0x5A],
X: [0x4D],
Y: [0x4E],
Z: [0x4F],
R: [0x50],
L: [0x51],
F9: [0x78],
CTRL: [0x11]
}
attr_reader :dir4, :dir8
def press?(sym)
KtK[sym].each {|s| return true if Key_States[s] > 0}
return false
end
def trigger?(sym)
KtK[sym].each {|s| return true if Key_States[s] == 1}
return false
end
def repeat?(sym)
KtK[sym].each {|s|
s = Key_States[s]
return true if s == 1 || s > 23 && s % 6 == 0
}
return false
end
#
def update
0xBE.times {|i|
GetAsyncKeyState.call(i) == 0 ? Key_States[i] = 0 : Key_States[i] += 1
}
update_dir_state
end
def update_dir_state
if press?(:DOWN)
if press?(:LEFT); @dir8 = 1
elsif press?(:RIGHT); @dir8 = 3
else @dir8 = 2
end
@dir4 = 2
elsif press?(:LEFT)
@dir8 = press?(:UP) ? 7 : 4
@dir4 = 4
elsif press?(:RIGHT)
@dir8 = press?(:UP) ? 9 : 6
@dir4 = 6
elsif press?(:UP)
@dir8 = 8
@dir4 = 8
else
@dir8 = 0
@dir4 = 0
end
end
end
昨天那个有问题,你怎么用起来没问题..