Click here to Skip to main content
15,888,803 members
Home / Discussions / C#
   

C#

 
GeneralRe: Programmatically Attach Debugger Pin
John Torjo26-Jan-16 20:54
professionalJohn Torjo26-Jan-16 20:54 
Questionpost and manipulate result JSON WebService Pin
Member 1228688325-Jan-16 12:58
Member 1228688325-Jan-16 12:58 
AnswerRe: post and manipulate result JSON WebService Pin
Garth J Lancaster25-Jan-16 13:33
professionalGarth J Lancaster25-Jan-16 13:33 
GeneralRe: post and manipulate result JSON WebService Pin
Member 1228688326-Jan-16 14:22
Member 1228688326-Jan-16 14:22 
GeneralRe: post and manipulate result JSON WebService Pin
Garth J Lancaster26-Jan-16 14:31
professionalGarth J Lancaster26-Jan-16 14:31 
QuestionRe-Assignable Keyboard Shortcuts Pin
Eiredrake25-Jan-16 11:05
Eiredrake25-Jan-16 11:05 
AnswerRe: Re-Assignable Keyboard Shortcuts Pin
John Torjo25-Jan-16 23:20
professionalJohn Torjo25-Jan-16 23:20 
AnswerRe: Re-Assignable Keyboard Shortcuts Pin
Pete O'Hanlon26-Jan-16 0:09
mvePete O'Hanlon26-Jan-16 0:09 
I would imagine that you're going to have a dictionary of some sort in there - and you'll map that to your commands. Off the top of my head, you'd do a two phase approach to building this up. In the first phase, you'd have your application commands (looking something like this):
C#
public class ApplicationCommand
{
  public char Shortcut { get; set; } // Assuming a single key entry here rather than key combinations
  public Action Command { get; set; }
}
You would populate these as a default stage (possibly in a dictionary), and then read through some configuration to remap those elements that need to be updated:
C#
public class CommandMap
{
  private Dictionary<string, ApplicationCommand> applicationCommands = new Dictionary<string, ApplicationCommand>();
  public void AddCommand(string application, ApplicationCommand command)
  {
    Add(application, command);
  }
}

public class MyGameLoop
{
  private CommandMap commandMap = new CommandMap();
  public void Initialize()
  {
    commandMap.AddCommand("Load", new ApplicationCommand('L', ()=> { Debug.WriteLine("Loading"); }));
    commandMap.AddCommand("Save", new ApplicationCommand('S', ()=> { Debug.WriteLine("Saving"); }));
  }
  public void UpdateEntries()
  {
    // Read in from your store here
    ConfigurationStoreReader reader = new ConfigurationStoreReader();
    foreach (Tuple<string, char> element in reader.CommandUpdateMap)
    {
      command[element.Item1] = element.Item2;
    }
  }
  public CommandMap Mapping { get { return command; } }
}
From that, we have everything in place that we need to expose a key press to Action map
C#
public class Game
{
  private Dictionary<char, Action> actionToPerform = new Dictionary<char, Action>();
  public Game(CommandMap mapping)
  {
    foreach (ApplicationCommand appCommand in mapping.Mapping)
    {
      actionToPerform.Add(appCommand.Shortcut, appCommand.Command);
    }
  }

  public void HandleKeyPress(char keyPress)
  {
    if (actionToPerform.ContainsKey(keyPress))
    {
      actionToPerform[keyPress]();
    } 
  }
}
I've just typed this up in the web editor here, so apologies if there are minor syntactic issues but this should serve to demonstrate what I'm talking about.
This space for rent

GeneralRe: Re-Assignable Keyboard Shortcuts Pin
Richard MacCutchan26-Jan-16 0:28
mveRichard MacCutchan26-Jan-16 0:28 
GeneralRe: Re-Assignable Keyboard Shortcuts Pin
Pete O'Hanlon26-Jan-16 0:52
mvePete O'Hanlon26-Jan-16 0:52 
QuestionData disappears on postback Pin
Carl Cummings25-Jan-16 7:21
Carl Cummings25-Jan-16 7:21 
AnswerRe: Data disappears on postback Pin
Richard Deeming25-Jan-16 8:51
mveRichard Deeming25-Jan-16 8:51 
QuestionHow to use WeakEventManager with reflection Pin
Kenneth Haugland23-Jan-16 23:21
mvaKenneth Haugland23-Jan-16 23:21 
SuggestionRe: How to use WeakEventManager with reflection Pin
Kornfeld Eliyahu Peter24-Jan-16 1:18
professionalKornfeld Eliyahu Peter24-Jan-16 1:18 
GeneralRe: How to use WeakEventManager with reflection Pin
Sascha Lefèvre24-Jan-16 1:34
professionalSascha Lefèvre24-Jan-16 1:34 
GeneralRe: How to use WeakEventManager with reflection Pin
Kornfeld Eliyahu Peter24-Jan-16 1:38
professionalKornfeld Eliyahu Peter24-Jan-16 1:38 
GeneralRe: How to use WeakEventManager with reflection Pin
Sascha Lefèvre24-Jan-16 1:42
professionalSascha Lefèvre24-Jan-16 1:42 
GeneralRe: How to use WeakEventManager with reflection Pin
Kenneth Haugland24-Jan-16 3:25
mvaKenneth Haugland24-Jan-16 3:25 
GeneralRe: How to use WeakEventManager with reflection Pin
Kornfeld Eliyahu Peter24-Jan-16 3:42
professionalKornfeld Eliyahu Peter24-Jan-16 3:42 
GeneralRe: How to use WeakEventManager with reflection Pin
Kenneth Haugland24-Jan-16 3:47
mvaKenneth Haugland24-Jan-16 3:47 
AnswerRe: How to use WeakEventManager with reflection Pin
Sascha Lefèvre24-Jan-16 2:37
professionalSascha Lefèvre24-Jan-16 2:37 
GeneralRe: How to use WeakEventManager with reflection Pin
Kenneth Haugland24-Jan-16 3:11
mvaKenneth Haugland24-Jan-16 3:11 
GeneralRe: How to use WeakEventManager with reflection Pin
Sascha Lefèvre24-Jan-16 4:33
professionalSascha Lefèvre24-Jan-16 4:33 
GeneralRe: How to use WeakEventManager with reflection Pin
Kenneth Haugland24-Jan-16 4:45
mvaKenneth Haugland24-Jan-16 4:45 
GeneralRe: How to use WeakEventManager with reflection Pin
Sascha Lefèvre24-Jan-16 6:14
professionalSascha Lefèvre24-Jan-16 6:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.