@@ -75,16 +75,18 @@ In general, the keyboard event naming follows the following rules:
7575
7676 ``"time-" + <key name> ``
7777
78- Here is an example of time reading in code:
78+ .. only :: python
7979
80- .. code-block :: python
80+ Here is an example of time reading in code:
81+
82+ .. code-block :: python
8183
82- class ReadKeys (DirectObject .DirectObject ):
83- def __init__ (self ):
84- self .accept(' time-a-repeat' , self .printRepeat)
84+ class ReadKeys (DirectObject .DirectObject ):
85+ def __init__ (self ):
86+ self .accept(' time-a-repeat' , self .printRepeat)
8587
86- def printRepeat (self , when ):
87- print (" repeat a" , when)
88+ def printRepeat (self , when ):
89+ print (" repeat a" , when)
8890
8991 6. Keys that don't type a character are labeled as follows::
9092
@@ -283,36 +285,75 @@ the GraphicsWindow object, returning a :class:`.ButtonMap` object, which can be
283285used to find out which virtual key event will be fired for a certain raw
284286keyboard button:
285287
286- .. code-block :: python
288+ .. only :: python
287289
288- # Get the current keyboard layout.
289- # This may be a somewhat expensive operation, so don't call
290- # it all the time, instead storing the result when possible.
291- map = base.win.get_keyboard_map()
290+ .. code-block :: python
291+
292+ # Get the current keyboard layout.
293+ # This may be a somewhat expensive operation, so don't call
294+ # it all the time, instead storing the result when possible.
295+ map = base.win.get_keyboard_map()
296+
297+ # Use this to print all key mappings
298+ print (map )
292299
293- # Use this to print all key mappings
294- print ( map )
300+ # Find out which virtual key is associated with the ANSI US "w"
301+ w_button = map .get_mapped_button( " w " )
295302
296- # Find out which virtual key is associated with the ANSI US "w"
297- w_button = map .get_mapped_button(" w" )
303+ # Get a textual representation for the button
304+ w_label = map .get_mapped_button_label(" w" )
305+ if not w_label:
306+ # There is none, use the event name instead.
307+ w_label = str (w_button)
308+ w_label = w_label.capitalize()
298309
299- # Get a textual representation for the button
300- w_label = map .get_mapped_button_label(" w" )
301- if w_label:
302- # There is none, use the event name instead.
303- w_label = str (w_button)
304- w_label = w_label.capitalize()
310+ # Use this label to tell the player which button to press.
311+ self .tutorial_text = " Press %s to move forward." % (w_label)
305312
306- # Use this label to tell the player which button to press.
307- self .tutorial_text = " Press %s to move forward." % (w_label)
313+ # Poll to check if the button is pressed...
314+ if base.mouseWatcherNode.is_button_down(w_button):
315+ print (" %s is currently pressed" % (w_label))
308316
309- # Poll to check if the button is pressed...
310- if base.mouseWatcherNode.is_button_down(w_button):
311- print (" %s is currently pressed" % (w_label))
317+ # ...or register event handlers
318+ self .accept(" %s " % (w_button), self .start_moving_forward)
319+ self .accept(" %s -up" % (w_button), self .stop_moving_forward)
320+
321+ .. only :: cpp
322+
323+ .. code-block :: cpp
324+
325+ // Get the current keyboard layout.
326+ // This may be a somewhat expensive operation, so don't call
327+ // it all the time, instead storing the result when possible.
328+ PT(ButtonMap) map = window->get_keyboard_map();
329+
330+ // Use this to print all key mappings
331+ map->write(std::cout);
332+
333+ // Find out which virtual key is associated with the ANSI US "w"
334+ ButtonHandle w_button = map.get_mapped_button("w");
335+
336+ // Get a textual representation for the button
337+ std::string w_label = map.get_mapped_button_label("w");
338+ if (w_label) {
339+ // There is none, use the event name instead.
340+ w_label = w_button.get_name();
341+ }
342+ w_label = downcase(w_label); // from string_utils.h
343+ w_label[0] = toupper(w_label[0]);
344+
345+ // Use this label to tell the player which button to press.
346+ std::ostringstream str;
347+ str << "Press " << w_label << " to move forward.";
348+
349+ // Poll to check if the button is pressed...
350+ if (mouse_watcher->is_button_down(w_button)) {
351+ std::cout << w_label << " is currently pressed" << std::endl;
352+ }
312353
313- # ...or register event handlers
314- self .accept( " %s " % (w_button), self . start_moving_forward)
315- self .accept( " %s -up" % (w_button), self . stop_moving_forward)
354+ // ...or register event handlers
355+ framework->define_key (w_button.get_name( ), start_moving_forward, nullptr);
356+ framework->define_key(w_button.get_name() + " -up", stop_moving_forward, nullptr);
316357
317358 The above code example also illustrates the use of the
318359:meth: `~.ButtonMap.get_mapped_button_label() ` function to get a textual
0 commit comments