Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

Strategy Keyboard Event?
 Post subject: Strategy Keyboard Event? Post rating: 0   New post Posted: Thu 17 Mar, 2016, 11:03 
User avatar

User rating: 0
Joined: Thu 17 Mar, 2016, 11:02
Posts: 1
Location: Canada,
Is there any snippets/code pertaining to Keyboard Events in relation to strategies?

I found an outdated and no longer existent post here: https://www.dukascopy.com/swiss/english ... ard#p66669

I have been piecing together some older work by user Jlongo, in order to try and grasp how to use keyboard events in strategies.

Any help appreciated.


 
 Post subject: Re: Strategy Keyboard Event? Post rating: 0   New post Posted: Mon 04 Apr, 2016, 11:52 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Consider this strategy.
Please note that KeyEventDispatcher.dispatchKeyEvnet is executed outside IStrategy thread, therefore API calls will not be executed from it.
To call API methods please use IContext.executeTask().
package support;

import com.dukascopy.api.*;
import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IEngine;
import com.dukascopy.api.IHistory;
import com.dukascopy.api.IMessage;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.Period;
import com.sun.jmx.snmp.tasks.Task;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import java.util.concurrent.Callable;

public class Keyboard implements IStrategy {
    private IEngine engine;
    private IHistory history;
    private IConsole console;

    @Override
    public void onStart(IContext context) throws JFException {
        this.console = context.getConsole();
        this.engine = context.getEngine();
        this.history = context.getHistory();
       
        final IContext finalContext = context;
       
        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
            public boolean dispatchKeyEvent(final KeyEvent e) {
                boolean discardEvent = false;
               
                int closeMask = KeyEvent.CTRL_DOWN_MASK + 82; //Ctrl+r

                if (((e.getModifiersEx() + e.getKeyCode()) == closeMask) && e.paramString().indexOf("KEY_PRESSED") >= 0) {
                    console.getOut().println("ctrl-r pressed");
                    finalContext.executeTask(new OnKeyTask());
                    discardEvent = true;

                }
                return discardEvent;
            }
        });
    }

    private class OnKeyTask implements Callable<Object> {

        public OnKeyTask() {
        }

        public Object call() throws Exception {
            console.getOut().println("task");
            return null;
        }
       
    }

    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onAccount(IAccount account) throws JFException {
    }

    public void onStop() throws JFException {
    }
}


 

Jump to:  

  © 1998-2025 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com