Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Changing the movemen keys

Hey, I know the defualt for makign your character move is the arrow keys, but I want to change it to the W,A,S,D keys, and I'm not the most brilliant RGSS scripter, but i've looked through the code and don't know what I'm supposed to change or not.

So if anyone knows, and can tell me, that would awesome  ;D
 

khmp

Sponsor

You need the help of an Input module or class it seems. There are plenty fortunately for you. Give the Submitted Scripts section a looksie or do a search. Pay particular attention to the Script Archive thread. If you still need help or can't find anything applicable just respond back here.

Good luck with it asmodeus! :afro:
 

poccil

Sponsor

Here is a rewritten Input module I made:

Code:
module Input
  DOWN=2
  LEFT=4
  RIGHT=6
  UP=8
  A=11
  B=12
  C=13
  X=14
  Y=15
  Z=16
  L=17
  R=18
  SHIFT=21
  CTRL=22
  ALT=23
  F5=25
  F6=26
  F7=27
  F8=28
  F9=29
  # GetAsyncKeyState or GetKeyState will work here
  @GetKeyState=Win32API.new("user32", "GetAsyncKeyState", "i", "i")
  # Returns whether a key is being pressed
  def self.getstate(key)
    return (@GetKeyState.call(key)&0x8000)>0
  end
  def self.updateex
   self.update
  end
  def self.update
    if @keystate
      for i in 0...256
        @newstate=self.getstate(i)
        @triggerstate[i]=(@newstate&&@keystate[i]==0)
        @releasestate[i]=(!@newstate&&@keystate[i]>0)
        @keystate[i]=@newstate ? @keystate[i]+1 : 0
      end    
    else
      @keystate=[]
      @triggerstate=[]
      @releasestate=[]
      for i in 0...256
        @keystate[i]=self.getstate(i) ? 1 : 0
        @triggerstate[i]=false
        @releasestate[i]=false
      end
    end
  end
  def self.buttonToKey(button)
   case button
    when Input::DOWN
     return [0x28] # Down
    when Input::LEFT
     return [0x25] # Left
    when Input::RIGHT
     return [0x27] # Right
    when Input::UP
     return [0x26] # Up
    when Input::A
     return [0x5A,0x10] # Z, Shift
    when Input::B
     return [0x58,0x1B] # X, ESC 
    when Input::C
     return [0x43,0x0d,0x20] # C, ENTER, Space
    when Input::X
     return [0x41] # A
    when Input::Y
     return [0x53] # S
    when Input::Z
     return [0x44] # D
    when Input::L
     return [0x51,0x21] # Q, Page Up
    when Input::R
     return [0x57,0x22] # W, Page Up
    when Input::SHIFT
     return [0x10] # Shift
    when Input::CTRL
     return [0x11] # Ctrl
    when Input::ALT
     return [0x12] # Alt
    when Input::F5
     return [0x74] # F5
    when Input::F6
     return [0x75] # F6
    when Input::F7
     return [0x76] # F7
    when Input::F8
     return [0x77] # F8
    when Input::F9
     return [0x78] # F9
    else
     return []
   end
  end
  def self.dir4
   button=0
   repeatcount=0
   if self.press?(Input::DOWN) && self.press?(Input::UP)
     return 0
   end
   if self.press?(Input::LEFT) && self.press?(Input::RIGHT)
     return 0
   end
   for b in [Input::DOWN,Input::LEFT,Input::RIGHT,Input::UP]
    rc=self.count(b)
    if rc>0
     if repeatcount==0 || rc<repeatcount
      button=b
      repeatcount=rc
     end
    end
   end
   return button
  end
  def self.dir8
   buttons=[]
   for b in [Input::DOWN,Input::LEFT,Input::RIGHT,Input::UP]
    rc=self.count(b)
    if rc>0
     buttons.push([b,rc])
    end
   end
   if buttons.length==0
    return 0
   elsif buttons.length==1
    return buttons[0][0]
   elsif buttons.length==2
    if (buttons[0][0]==Input::DOWN && buttons[1][0]==Input::UP) ||
       (buttons[0][0]==Input::UP && buttons[1][0]==Input::DOWN)
     return 0
    end
    if (buttons[0][0]==Input::LEFT && buttons[1][0]==Input::RIGHT) ||
       (buttons[0][0]==Input::RIGHT && buttons[1][0]==Input::LEFT)
     return 0
    end
   end
   buttons.sort!{|a,b| a[1]<=>b[1]}
   updown=0
   leftright=0
   for b in buttons
    if updown==0 && (b[0]==Input::UP || b[0]==Input::DOWN)
     updown=b[0]
    end
    if leftright==0 && (b[0]==Input::LEFT || b[0]==Input::RIGHT)
     leftright=b[0]
    end
   end
   if updown==Input::DOWN
    return 1 if leftright==Input::LEFT
    return 3 if leftright==Input::RIGHT
    return 2
   elsif updown==Input::UP
    return 7 if leftright==Input::LEFT
    return 9 if leftright==Input::RIGHT
    return 8
   else
    return 4 if leftright==Input::LEFT
    return 6 if leftright==Input::RIGHT
    return 0
   end
  end
  def self.trigger?(button)
   return self.buttonToKey(button).any? {|item| self.triggerex?(item) }
  end
  def self.repeat?(button)
   return self.buttonToKey(button).any? {|item| self.repeatex?(item) }
  end
  def self.count(button)
   for btn in self.buttonToKey(button)
     c=self.repeatcount(btn)
     return c if c>0
   end
   return 0
  end 
  def self.press?(button)
   return self.buttonToKey(button).any? {|item| self.pressex?(item) }
  end
  def self.repeatex?(key)
    return false if !@keystate
    return @keystate[key]==1 || (@keystate[key]>20 && (@keystate[key]&1)==0)
  end
  def self.releaseex?(key)
    return false if !@releasestate
    return @releasestate[key]
  end
  def self.triggerex?(key)
    return false if !@triggerstate
    return @triggerstate[key]
  end
  def self.repeatcount(key)
    return 0 if !@keystate
    return @keystate[key]
  end
  def self.pressex?(key)
    return self.repeatcount(key)>0
  end
end

To make that code do what you want, you can change it like this:
  def self.buttonToKey(button)
  case button
    when Input::DOWN
    return [0x28,0x53] # Down, S
    when Input::LEFT
    return [0x25,0x41] # Left, A
    when Input::RIGHT
    return [0x27,0x44] # Right, D
    when Input::UP
    return [0x26,0x57] # Up, W
 
poccil":39wpg1eu said:
Here is a rewritten Input module I made:

Code:
module Input
  DOWN=2
  LEFT=4
  RIGHT=6
  UP=8
  A=11
  B=12
  C=13
  X=14
  Y=15
  Z=16
  L=17
  R=18
  SHIFT=21
  CTRL=22
  ALT=23
  F5=25
  F6=26
  F7=27
  F8=28
  F9=29
  # GetAsyncKeyState or GetKeyState will work here
  @GetKeyState=Win32API.new("user32", "GetAsyncKeyState", "i", "i")
  # Returns whether a key is being pressed
  def self.getstate(key)
    return (@GetKeyState.call(key)&0x8000)>0
  end
  def self.updateex
   self.update
  end
  def self.update
    if @keystate
      for i in 0...256
        @newstate=self.getstate(i)
        @triggerstate[i]=(@newstate&&@keystate[i]==0)
        @releasestate[i]=(!@newstate&&@keystate[i]>0)
        @keystate[i]=@newstate ? @keystate[i]+1 : 0
      end    
    else
      @keystate=[]
      @triggerstate=[]
      @releasestate=[]
      for i in 0...256
        @keystate[i]=self.getstate(i) ? 1 : 0
        @triggerstate[i]=false
        @releasestate[i]=false
      end
    end
  end
  def self.buttonToKey(button)
   case button
    when Input::DOWN
     return [0x28] # Down
    when Input::LEFT
     return [0x25] # Left
    when Input::RIGHT
     return [0x27] # Right
    when Input::UP
     return [0x26] # Up
    when Input::A
     return [0x5A,0x10] # Z, Shift
    when Input::B
     return [0x58,0x1B] # X, ESC 
    when Input::C
     return [0x43,0x0d,0x20] # C, ENTER, Space
    when Input::X
     return [0x41] # A
    when Input::Y
     return [0x53] # S
    when Input::Z
     return [0x44] # D
    when Input::L
     return [0x51,0x21] # Q, Page Up
    when Input::R
     return [0x57,0x22] # W, Page Up
    when Input::SHIFT
     return [0x10] # Shift
    when Input::CTRL
     return [0x11] # Ctrl
    when Input::ALT
     return [0x12] # Alt
    when Input::F5
     return [0x74] # F5
    when Input::F6
     return [0x75] # F6
    when Input::F7
     return [0x76] # F7
    when Input::F8
     return [0x77] # F8
    when Input::F9
     return [0x78] # F9
    else
     return []
   end
  end
  def self.dir4
   button=0
   repeatcount=0
   if self.press?(Input::DOWN) && self.press?(Input::UP)
     return 0
   end
   if self.press?(Input::LEFT) && self.press?(Input::RIGHT)
     return 0
   end
   for b in [Input::DOWN,Input::LEFT,Input::RIGHT,Input::UP]
    rc=self.count(b)
    if rc>0
     if repeatcount==0 || rc<repeatcount
      button=b
      repeatcount=rc
     end
    end
   end
   return button
  end
  def self.dir8
   buttons=[]
   for b in [Input::DOWN,Input::LEFT,Input::RIGHT,Input::UP]
    rc=self.count(b)
    if rc>0
     buttons.push([b,rc])
    end
   end
   if buttons.length==0
    return 0
   elsif buttons.length==1
    return buttons[0][0]
   elsif buttons.length==2
    if (buttons[0][0]==Input::DOWN && buttons[1][0]==Input::UP) ||
       (buttons[0][0]==Input::UP && buttons[1][0]==Input::DOWN)
     return 0
    end
    if (buttons[0][0]==Input::LEFT && buttons[1][0]==Input::RIGHT) ||
       (buttons[0][0]==Input::RIGHT && buttons[1][0]==Input::LEFT)
     return 0
    end
   end
   buttons.sort!{|a,b| a[1]<=>b[1]}
   updown=0
   leftright=0
   for b in buttons
    if updown==0 && (b[0]==Input::UP || b[0]==Input::DOWN)
     updown=b[0]
    end
    if leftright==0 && (b[0]==Input::LEFT || b[0]==Input::RIGHT)
     leftright=b[0]
    end
   end
   if updown==Input::DOWN
    return 1 if leftright==Input::LEFT
    return 3 if leftright==Input::RIGHT
    return 2
   elsif updown==Input::UP
    return 7 if leftright==Input::LEFT
    return 9 if leftright==Input::RIGHT
    return 8
   else
    return 4 if leftright==Input::LEFT
    return 6 if leftright==Input::RIGHT
    return 0
   end
  end
  def self.trigger?(button)
   return self.buttonToKey(button).any? {|item| self.triggerex?(item) }
  end
  def self.repeat?(button)
   return self.buttonToKey(button).any? {|item| self.repeatex?(item) }
  end
  def self.count(button)
   for btn in self.buttonToKey(button)
     c=self.repeatcount(btn)
     return c if c>0
   end
   return 0
  end 
  def self.press?(button)
   return self.buttonToKey(button).any? {|item| self.pressex?(item) }
  end
  def self.repeatex?(key)
    return false if !@keystate
    return @keystate[key]==1 || (@keystate[key]>20 && (@keystate[key]&1)==0)
  end
  def self.releaseex?(key)
    return false if !@releasestate
    return @releasestate[key]
  end
  def self.triggerex?(key)
    return false if !@triggerstate
    return @triggerstate[key]
  end
  def self.repeatcount(key)
    return 0 if !@keystate
    return @keystate[key]
  end
  def self.pressex?(key)
    return self.repeatcount(key)>0
  end
end

To make that code do what you want, you can change it like this:
  def self.buttonToKey(button)
   case button
    when Input::DOWN
     return [0x28,0x53] # Down, S
    when Input::LEFT
     return [0x25,0x41] # Left, A
    when Input::RIGHT
     return [0x27,0x44] # Right, D
    when Input::UP
     return [0x26,0x57] # Up, W

Thanks! That was exactly what I needed, and it works perfectly!
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top