#==============================================================================
# ** Keyboard Input Module (Revised)
#------------------------------------------------------------------------------
# by DerVVulfman
# version 1.2
# 08-15-2007
#------------------------------------------------------------------------------
# Based on...
# Keyboard Input Module version 3
# by Near Fantastica (06.07.05)
#==============================================================================
=begin
Usage:
Instructions
This script is used to detect keys being pressed and return them for use in your scripts... whether it is a message script, a custom menu system or advanced battlesystem. This script alone accomplishes nothing unless you create a system that utilizes the calls within:
Syntax (Just like Near Fantastica's - with one addition.)
Code:
if Input.get_function == "function" # Keys like [Esc], [Ctrl], [Tab]
if Input.get_letters == "letter" # The alphabet keys (augmented by (Shift] or [Caps])
if Input.get_numbers == "number" # The number keys (disabled if [Shift] is being pressed)
if Input.get_key == "char" # All other printable keys (and top-row number keys [shift]ed)
if Input.get_direction == "number" # Number Pad & [Home], [PgUp] and etc.
and
if Input.get_individual == "function" # Keys like [Left Alt], [Right Alt], [Left Shift]
Input.get_function
[SIZE="1]This call allows you to detect specialized keys such as 'Esc', 'Ctrl', 'Backspace' , 'Print Screen' and the 'F1 to F12' keys
[/SIZE]
Input.get_letters
This call allows you to detect the alphabetic characters being returned, and whether they are 'shifted' or not. Unlike cybersam's system, this detects whether the left and/or right shift buttons are being pressed so generation of capital and lowercase characters can be produced.
Input.get_numbers
This call allows you to detect the number keys (0 to 9) along the TOP of the keyboard. It does not detect any 'shifted' characters and does not detect the number keys on the keypad.
Input.get_key
This call returns most other alpha-numeric characters such as the space key, brackets and parenthesis, equals and the 'shifted' characters along the top of the keyboard.
Input.get_direction
This call returns the number keys on the directional keypad when the Numberlock is on and directional keys when it is off. It also returns keys such as PgUp, PgDown, Home and etc.
Input.get_individual
This call returns the specialized function keys of [CapsLock], [NumberLock] and [ScrollLock]. But it also detects the unique keys such as [Lf Ctrl] and [Rt Cntl] and matching ones for Alt and Shift. The pressing of [CapsLock] and [NumberLock] automatically alters the returned values of the other functions, but this function is useful if you want to detect and return the button as part of your special menu/battle/whatever system.
=end
module Input
#--------------------------------------------------------------------------
# * Get Current Keypress State (Regular presses)
#--------------------------------------------------------------------------
def Input.getstate(key)
return true unless Win32API.new("user32","GetKeyState",["i"],"i").call(key).between?(0, 1)
return false
end
#--------------------------------------------------------------------------
# * Get Continuous Keyboard State (CapsLock, NumLock, ScrollLock)
#--------------------------------------------------------------------------
def Input.keyboardstate(rkey, key = 0)
return false unless Win32API.new("user32","GetKeyState",['i'],'i').call(rkey) & 0x01 == key
return true
end
#--------------------------------------------------------------------------
# * Numberpad Input System
#--------------------------------------------------------------------------
def Input.get_direction
return "Center" if Input.getstate(12)
return "PgUp" if Input.getstate(33)
return "PgDn" if Input.getstate(34)
return "End" if Input.getstate(35)
return "Home" if Input.getstate(36)
return "Left" if Input.getstate(37)
return "Up" if Input.getstate(38)
return "Right" if Input.getstate(39)
return "Down" if Input.getstate(40)
return "Ins" if Input.getstate(45)
return "Del" if Input.getstate(46)
return "*" if Input.getstate(106)
return "+" if Input.getstate(107)
return "-" if Input.getstate(109)
return "/" if Input.getstate(111)
if Input.keyboardstate(144,1)
for key in 96..105 #ASCII KEYS 48(0) to 57(9)
if Input.getstate(key)
return (key-48).chr
end
end
end
return nil
end
#--------------------------------------------------------------------------
# * Function Keys (Tab / Enter / Ctrl / Etc)
#--------------------------------------------------------------------------
def Input.get_function
return "Back" if Input.getstate(8)
if Input.getstate(16)
return "BackTab" if Input.getstate(9)
else
return "Tab" if Input.getstate(9)
end
return "Enter" if Input.getstate(13)
return "Ctrl" if Input.getstate(17)
return "Alt" if Input.getstate(18)
return "Pause" if Input.getstate(19)
return "Esc" if Input.getstate(27)
return "PrntScr" if Input.getstate(44)
# Function Keys (F1 - F12)
for key in 112..123
if Input.getstate(key)
num = key - 111
return "F" + num.to_s
end
end
return nil
end
#--------------------------------------------------------------------------
# * Individual Keys (Left OR Right keys, and Windows & Application keys)
#--------------------------------------------------------------------------
def Input.get_individual
return "CapsLock" if Input.getstate(20)
return "NumLock" if Input.getstate(144)
return "ScrlLock" if Input.getstate(145)
return "Lf Win" if Input.getstate(91)
return "Rt Win" if Input.getstate(92)
return "App Key" if Input.getstate(93)
return "Lf Shift" if Input.getstate(160)
return "Rt Shift" if Input.getstate(161)
return "Lf Ctrl" if Input.getstate(162)
return "Rt Ctrl" if Input.getstate(163)
return "Lf Alt" if Input.getstate(164)
return "Rt Alt" if Input.getstate(165)
return nil
end
#--------------------------------------------------------------------------
# * Letter Keys (Just your regular alphabet, caps or otherwise)
#--------------------------------------------------------------------------
def Input.get_letters
for key in 65..90 #ASCII KEYS 65(a) to 90(z)
if Input.getstate(key)
# Ensure lower-case if NEITHER or BOTH the shift and caps are pressed
if (Input.getstate(16) and Input.keyboardstate(20,1) ) or
(Input.getstate(16) != true and Input.keyboardstate(20,1) != true)
return key.chr.downcase
else
# Otherwise, uppercase
return key.chr.upcase
end
end
end
return nil
end
#--------------------------------------------------------------------------
# * Number Keys (only the top keys)
#--------------------------------------------------------------------------
def Input.get_numbers
return nil if Input.getstate(16)
for key in 48..57 #ASCII KEYS 48(0) to 57(9)
if Input.getstate(key)
return key.chr
end
end
return nil
end
end