File: 11_keys_codes.c

package info (click to toggle)
mlv 3.1.0-8
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 10,596 kB
  • sloc: ansic: 18,982; sh: 4,760; makefile: 381; objc: 246; xml: 92
file content (105 lines) | stat: -rw-r--r-- 2,848 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#ifndef _GNU_SOURCE
	#define _GNU_SOURCE
#endif
#include <stdio.h>

#if defined( __WIN32__ ) || defined( _WIN32 ) || defined( __CYGWIN__ )
extern int asprintf (char **, const char *, ...);
#endif

#include <stdlib.h>
#include <string.h>
#include <MLV/MLV_all.h>

void draw_sentence( int width, int height ){
	MLV_clear_window( MLV_COLOR_BLACK );
	MLV_draw_adapted_text_box(
		10, 50, 
		"Appuyez sur une touche pour connaître :\n    - son code,\n    - son mode,\n    - son code unicode,\n    - le caractère qui lui est associé.",
		3,
		MLV_COLOR_BLACK, MLV_COLOR_GREEN, MLV_COLOR_BLACK,
		MLV_TEXT_LEFT
	);
}

//
// Attention ! 
// Pour pouvoir compiler ce programme sous windows et sous macintosh,
// il faut, pour la déclaration du main, respecter strictement la syntaxe
// suivante :
//
int main( int argc, char *argv[] ){

	int width = 640;
	int height = 480;

	MLV_create_window( "beginner - 10 - Le code des touches du clavier", "codes - touches du clavier", width, height );

	draw_sentence( width, height );
	MLV_update_window();

	MLV_Keyboard_button sym;
	const char* sym_string;
	MLV_Keyboard_modifier mod;
	char* mod_string;
	int unicode;
	char* character;
	char* text;

	while( 1 ){
		MLV_wait_keyboard( &sym, &mod, &unicode );
		sym_string = MLV_convert_keyboard_button_to_string( sym );
		mod_string = MLV_convert_keyboard_mod_to_string( mod );
		character = MLV_convert_unicode_to_string( unicode );

		if(
			asprintf(
				&text, 
				"Information sur la touche:\n-------------------------\nSym : %s \nMod : %s \nUnicode : %d \nCaractère : %s \n\n",
				sym_string, mod_string, unicode, character
			) == -1
		){
			fprintf( stderr, "Allocation Error - file : %s, line : %d \n", __FILE__, __LINE__);
			exit(1);
		};

		printf( "%s", text );

		draw_sentence( width, height );
		MLV_draw_adapted_text_box(
			10, 160, text, 6,
			MLV_COLOR_BLACK, MLV_COLOR_GREEN, MLV_COLOR_BLACK,
			MLV_TEXT_LEFT
		);
		MLV_update_window();

		free( character );
		free( mod_string );
		free( text );
	
	}

	MLV_free_window();

	return 0;
}

/*
 *   This file is part of the MLV Library.
 *
 *   Copyright (C) 2010-2014 Adrien Boussicault, Marc Zipstein
 *
 *
 *    This Library is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This Library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this Library.  If not, see <http://www.gnu.org/licenses/>.
 */