Autor Wątek:  Driving with joystick  (Przeczytany 14409 razy)

0 użytkowników i 1 Gość przegląda ten wątek.

Offline Greno Zee

  • Wiadomości: 33
    • Zobacz profil
  • Otrzymane polubienia: 1
Driving with joystick
« dnia: 22 Maja 2007, 22:22:19 »
Even though the control of MaSzyna are well organized for train driving, since I play only from laptop, I wondered how I could simplify the numpad controls (the Fn key combinations are not so wow).
My other reason was watching all those handle controls in the cabins and having a sort of handle controller (joystick) next to me just wasted - it simply called for a solution.

To use a joystick with MaSzyna you need AutoHotkey. Just download the installer from here. The installation process is plain simple.
Once installed, create anywhere on your PC (ideally in the MaSzyna folder) a file with extension AHK - say "MaSzyna Joystick.ahk".
Now copy this code into the file:
; AutoHotkey Version: 1.x
; Language:       English
; Platform:       Win9x/NT
; Author:         Greno Zee
;
; Script Function:
;   Provides joystick control for MaSzyna train simulator
;

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Persistent  ; Keep this script running until the user explicitly exits it.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
CheckInterval = 10 ; Number of miliseconds between joystick status checks. It's prefered not to change this value
ChecksForShift = 50 ; Number of check the joystick must be in certain position to trigger the SHIFT + Key input.

ShiftCounter := 0
SetTimer, WatchAxis, %CheckInterval%
return

WatchAxis:
GetKeyState, JoyX, JoyX  ; Get position of X axis.
GetKeyState, JoyY, JoyY  ; Get position of Y axis.
GetKeyState, Joy1, Joy1  ; Get button 1 state.
GetKeyState, Joy2, Joy2  ; Get button 2 state.

KeyToHoldDownPrev = %KeyToHoldDown%  ; Prev now holds the key that was down before (if any).

if JoyX > 70
{
    if Joy1 = D
        KeyToHoldDown = Numpad7
    else if Joy2 = D
        KeyToHoldDown = NumpadDot
    else
        KeyToHoldDown = Numpad9
}
else if JoyX < 30
{
    if Joy1 = D
        KeyToHoldDown = Numpad1
    else if Joy2 = D
        KeyToHoldDown = Numpad0
    else
        KeyToHoldDown = Numpad3
}
else if JoyY > 70
{
    if Joy1 = D
        KeyToHoldDown = NumpadMult
    else if Joy2 = D
        KeyToHoldDown = r
    else
        KeyToHoldDown = NumpadSub
}
else if JoyY < 30
{
    if Joy1 = D
        KeyToHoldDown = NumpadDiv
    else if Joy2 = D
        KeyToHoldDown = d
    else
        KeyToHoldDown = NumpadAdd
}
else
    KeyToHoldDown =

SetKeyDelay -1  ; Avoid delays between keystrokes.

if KeyToHoldDown   ; There is a key to press down.
{
    if KeyToHoldDown = %KeyToHoldDownPrev%
    {
        ShiftCounter += 1

        if ShiftCounter = %ChecksForShift%
        {
            SendInput {Shift Down}
            Sleep 70
            SendInput {%KeyToHoldDown%}
            Sleep 70
            SendInput {Shift Up}
        }
    }
    else
        SendInput {%KeyToHoldDown%}
}
else
    ShiftCounter := 0
return


Then double-click the file, start MaSzyna and enjoy!

The joystick controls are like this:
Forward: master controller +
Forward + button 1: field shunt +
Forward + button 2: reverser forward

Backward: master controller -
Backward + button 1: field shunt -
Backward + button 2: reverser backward

Left: extend main brake
Left + button 1: extend aid brake
Left + button 2: emergency brake

Right: release main brake
Right + button 1: release aid brake
Right + button 2: main brake quick release.

Holding the joystick in certain position does not cause any controls to be repeated. Instead, after half a second (this is an easily adjustable value) in certain position the script send a SHIFT + Key command.
So for instance if you hold the button 1 and push forward, the field shunt will jump one position forwards and after half a second it jumps to max.
These scripts are easy to adjust. If you figure out a better mapping, post here please so we can extend our driving experience!

Offline iwan

  • Administrator
  • Wiadomości: 596
    • Zobacz profil
    • Fotki na TWB
  • Otrzymane polubienia: 30
Re: Driving with joystick
« Odpowiedź #1 dnia: 28 Maja 2007, 22:37:42 »
Cytat: "Greno Zee"
To use a joystick with MaSzyna you need AutoHotkey.


Good job! Thanks!

Veľmi presný článok ;) Díky
/Krzysztof Waszkiewicz/ <iwan@eu07.pl> http://www.eu07.pl
tel: +48667841589
********* Non sunt multiplicanda entia sine necessitate *********

Offline Greno Zee

  • Wiadomości: 33
    • Zobacz profil
  • Otrzymane polubienia: 1
Driving with joystick
« Odpowiedź #2 dnia: 29 Maja 2007, 15:30:35 »
Even though I perfectly understand this last request, I can't satisfy it. But I'm sure someone else can put a translated copy to the polish section.

Btw, I'm gaining some experience with this and I'll probably tweak the script a bit. I plan on making the brake control respond only once per joystick move and the same probably for the field shunt.

Offline Greno Zee

  • Wiadomości: 33
    • Zobacz profil
  • Otrzymane polubienia: 1
Driving with joystick
« Odpowiedź #3 dnia: 30 Maja 2007, 16:17:08 »
Modified the script in original post to provide more intuitive control. Basically, repetitive keystrokes have been removed and holding the joystick in certain position causes the SHIFT + Key to be pressed.

Offline RooteK

  • Wiadomości: 1246
    • Zobacz profil
    • Nieoficjalna strona węzła kolejowego Jaworzno.
  • Otrzymane polubienia: 27
Driving with joystick
« Odpowiedź #4 dnia: 01 Lipca 2007, 19:09:34 »
Can you read, what to drive with steering wheel?? I don't have a Joystick. I have steering wheel.

Offline Greno Zee

  • Wiadomości: 33
    • Zobacz profil
  • Otrzymane polubienia: 1
Driving with joystick
« Odpowiedź #5 dnia: 02 Lipca 2007, 02:02:41 »
I'm affraid steering wheel combined with hotkeys will hardly work for you. The deal is HotKeys allows you to simulate key presses with a joystick. So first, you would need your wheel to stay in whatever position you set it to, which they tend not to and second you would need to use completely different logic in the hotkeys script. Something like remembering the current value and if the the X axis value reaches certain difference, you'd change up or down. But you also would need a separate script for each loco since they have different amount of positions.
If you ask me, joystick really works lot better, I've driven hundreds of kilometers with it and it just feels right to me. Get one, they cost like 10€.

Offline MariensPL

  • Wiadomości: 64
    • Zobacz profil
  • Otrzymane polubienia: 0
Odp: Driving with joystick
« Odpowiedź #6 dnia: 04 Kwietnia 2010, 16:08:40 »
Sorry, could you tell me how to configure this code or smoething for using a 'gamepad'?
You've seen the butcher.

Offline Greno Zee

  • Wiadomości: 33
    • Zobacz profil
  • Otrzymane polubienia: 1
Odp: Driving with joystick
« Odpowiedź #7 dnia: 04 Kwietnia 2010, 17:33:54 »
I haven't used this scripting engine for ages so I'm not sure anymore about the details.
I also never had a gamepad but I suppose it registers in your system as any game device. If that's the case, you can use this script without any modifications and it should work.