Click here to Skip to main content
15,913,027 members
Home / Discussions / C#
   

C#

 
GeneralRe: Add WeakEvent to ObservableCollection using reflection Pin
Kenneth Haugland27-Jan-16 22:09
mvaKenneth Haugland27-Jan-16 22:09 
AnswerRe: Add WeakEvent to ObservableCollection using reflection Pin
BillWoodruff27-Jan-16 14:52
professionalBillWoodruff27-Jan-16 14:52 
GeneralRe: Add WeakEvent to ObservableCollection using reflection Pin
Kenneth Haugland27-Jan-16 22:13
mvaKenneth Haugland27-Jan-16 22:13 
QuestionProgrammatically Attach Debugger Pin
Kevin Marois26-Jan-16 7:49
professionalKevin Marois26-Jan-16 7:49 
AnswerRe: Programmatically Attach Debugger Pin
OriginalGriff26-Jan-16 8:26
mveOriginalGriff26-Jan-16 8:26 
GeneralRe: Programmatically Attach Debugger Pin
Kevin Marois26-Jan-16 8:32
professionalKevin Marois26-Jan-16 8:32 
GeneralRe: Programmatically Attach Debugger Pin
OriginalGriff26-Jan-16 8:41
mveOriginalGriff26-Jan-16 8:41 
GeneralRe: Programmatically Attach Debugger Pin
Kevin Marois26-Jan-16 8:38
professionalKevin Marois26-Jan-16 8:38 
GeneralRe: Programmatically Attach Debugger Pin
OriginalGriff26-Jan-16 8:41
mveOriginalGriff26-Jan-16 8:41 
GeneralRe: Programmatically Attach Debugger Pin
Kevin Marois26-Jan-16 8:45
professionalKevin Marois26-Jan-16 8:45 
GeneralRe: Programmatically Attach Debugger Pin
OriginalGriff26-Jan-16 8:57
mveOriginalGriff26-Jan-16 8:57 
GeneralRe: Programmatically Attach Debugger Pin
Kevin Marois26-Jan-16 9:01
professionalKevin Marois26-Jan-16 9:01 
GeneralRe: Programmatically Attach Debugger Pin
OriginalGriff26-Jan-16 9:16
mveOriginalGriff26-Jan-16 9:16 
GeneralRe: Programmatically Attach Debugger Pin
Kevin Marois26-Jan-16 9:19
professionalKevin Marois26-Jan-16 9:19 
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 

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.